tags:

views:

5645

answers:

2

I have a JTable in which i set the column sized like following,

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.getColumnModel().getColumn(0).setPreferredWidth(27);
    table.getColumnModel().getColumn(1).setPreferredWidth(120);
    table.getColumnModel().getColumn(2).setPreferredWidth(100);
    table.getColumnModel().getColumn(3).setPreferredWidth(90);
    table.getColumnModel().getColumn(4).setPreferredWidth(90);
    table.getColumnModel().getColumn(6).setPreferredWidth(120);
    table.getColumnModel().getColumn(7).setPreferredWidth(100);
    table.getColumnModel().getColumn(8).setPreferredWidth(95);
    table.getColumnModel().getColumn(9).setPreferredWidth(40);
    table.getColumnModel().getColumn(10).setPreferredWidth(400);

This works fine but when the table is maximized i get empty space right of the last column is it possible to make the last column resize to the end on the window length when resized.

I found AUTO_RESIZE_LAST_COLUMN property in docs but it does not work.

Edit: JTable is in a JScrollPane its prefered size is set.

+2  A: 

What happens if you call setMinWidth(400) on the last column instead of setPreferredWidth(400)?

In the JavaDoc for JTable, read the docs for doLayout() very carefully. Here are some choice bits:

When the method is called as a result of the resizing of an enclosing window, the resizingColumn is null. This means that resizing has taken place "outside" the JTable and the change - or "delta" - should be distributed to all of the columns regardless of this JTable's automatic resize mode.

This might be why AUTO_RESIZE_LAST_COLUMN didn't help you.

Note: When a JTable makes adjustments to the widths of the columns it respects their minimum and maximum values absolutely.

This says that you might want to set Min == Max for all but the last columns, then set Min = Preferred on the last column and either not set Max or set a very large value for Max.

Eddie
setMinWidth(400) still causes the right of the table to be empty
Hamza Yerlikaya
Try adding setMaxWidth(10000) to see if that makes a difference.
Eddie
changed it but no effect.
Hamza Yerlikaya
Remove the line table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);This may prevent resizing that you want.
Eddie
You probably also want to set min and max size on the JTable itself, if you haven't already,
Eddie
A: 

With JTable.AUTO_RESIZE_OFF, the table will not change the size of any of the columns for you, so it will take your preferred setting. If it is your goal to have the columns default to your preferred size, except to have the last column fill the rest of the pane, You have the option of using the JTable.AUTO_RESIZE_LAST_COLUMN autoResizeMode, but it might be most effective when used with TableColumn.setMaxWidth() instead of TableColumn.setPreferredWidth() for all but the last column.

Once you are satisfied that AUTO_RESIZE_LAST_COLUMN does in fact work, you can experiment with a combination of TableColumn.setMaxWidth() and TableColumn.setMinWidth()

akf
When AUTO_RESIZE_LAST_COLUMN is used table is resized to fit the scroll pane my goal is to have the table at certain min size but make it grow if it is bigger than my mininum size.
Hamza Yerlikaya
You can have this behavior if you use TableColumn.setMinWidth() instead of TableColumn.setMaxWidth().
akf
to add to my last comment: use TableColumn.setMinWidth() for each column in your table, including the last column.
akf
setting both min and max width solved the problem but now scroll bars are missin.
Hamza Yerlikaya