Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto resizing the JTable column widths

I need my JTable to automatically re-size its column widths to fit the content. I found the TableColumnAdjuster class very useful. But there's a small problem. Say i have 5 columns, and their content is very short. In that case, if i use the auto adjuster, it sets the first four columns widths according to their content and gives all the rest of space to the last column. Please see the example.

enter image description here

Here the last column, Balance is given all the excess space. But what if i need to give that space to one of the middle columns. In the above case, i need to assign that space to the third column, name. I tried modifying the TableColumnAdjuster class's adjustColumns() method. But I couldn't get it working.

I tried both column.setPreferredWidth() and column.setWidth() for changing column sizes. But seems it doesn't change anything. How can I effectively change the column sizes of a JTable. If there's some other alternative or a direct answer to my main problem, that's better. Thanks!

like image 501
Anubis Avatar asked Jul 13 '13 05:07

Anubis


People also ask

How do I change the width of a JTable in Java?

By default the width of a JTable is fixed, we can also change the width of each column by using table. getColumnModel(). getColumn(). setPreferredWidth() method of JTable class.

How do I resize a table in Netbeans?

In design view, right-click over the table; Choose "Table Contents ..." and a Customizer Dialog will appear. Click the "Columns" tab, and there you will be able to set several properties, including the preferred / minimum / maximum width.

How do you make a JTable column not editable?

Right-click on the table cells. From popup menu, choose "Table Contents..". Uncheck the editable check box for the column you want to make it non-editable.


2 Answers

You can try the next:

public void resizeColumnWidth(JTable table) {     final TableColumnModel columnModel = table.getColumnModel();     for (int column = 0; column < table.getColumnCount(); column++) {         int width = 15; // Min width         for (int row = 0; row < table.getRowCount(); row++) {             TableCellRenderer renderer = table.getCellRenderer(row, column);             Component comp = table.prepareRenderer(renderer, row, column);             width = Math.max(comp.getPreferredSize().width +1 , width);         }         if(width > 300)             width=300;         columnModel.getColumn(column).setPreferredWidth(width);     } } 

JTable

This needs to be executed before the resize method.
If you have:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

JTable

like image 127
Paul Vargas Avatar answered Sep 19 '22 06:09

Paul Vargas


There is no option to automatically resize one column larger than the other.

Maybe you can to something like:

tca = new TableColumnAdjuster( table, 0 ); tca.adjustColumns(); TableColumnModel tcm = table.getColumnModel();   TableColumn tc = tcm.getColumn(1); tc.setWidth(tc.getWidth() + 25); 

This would allow you to add extra space to column 1. This extra space would only be added the first time the table is displayed.

Another option is to use:

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 

This would allocate extra space proportionally to each column.

like image 24
camickr Avatar answered Sep 17 '22 06:09

camickr