Stese
Stese

Reputation: 285

Auto resize 1 column, leaving another to take the rest of the available space. Using JTable in a JScrollPane

Auto resize 1 column, leaving another to take the rest of the available space. Using JTable in a JScrollPane.

I've been looking around for this for a while, and I can't seem to find an example that does this.

I've got a JTable in a ScrollPane, with 2 columns, and many rows. The first column only shows a small amount of data which is usually the same length, while the in the second, the length can vary quite a bit.

I'd like to resize the first column to fit the data, and then allow the second column to use up the rest of the space. I don't want it equally distributed across the panel.

I've seen examples of setting one column to Auto Resize, but what will that do with the second column?

Any help would be appreciated!

Thanks.

Steve

Upvotes: 0

Views: 185

Answers (1)

Stese
Stese

Reputation: 285

it seems that the setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); along with TableColumnAdjuster, does what I need it to do.

the code is as such

private JScrollPane textPanel() {
    String header[] = {"Date/Time","Log Entry"};

    String data[][] = 
    {       
            {"18-08-2014 - 16:21","Something"},
            {"18-08-2014 - 16:21","Something"},
            {"18-08-2014 - 16:21","Something"}  
    };

    JTable tPanel = new JTable(data,header);

    tPanel.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 14));

    tPanel.setGridColor(Color.BLACK);
    tPanel.setForeground(Color.WHITE);
    tPanel.setBackground(Color.BLACK);
    tPanel.getTableHeader().setResizingAllowed(false);
    tPanel.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

    TableColumnAdjuster tca = new TableColumnAdjuster(tPanel);
    tca.adjustColumns();

    JScrollPane sPanel;

    sPanel = new JScrollPane(tPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    sPanel.getViewport().setBackground(Color.BLACK);

    return sPanel;

Upvotes: 2

Related Questions