Link to home
Start Free TrialLog in
Avatar of Vanavah Edwards
Vanavah Edwards

asked on

How do I set the column width for selected columns in a JTable

For this line --->  table.getColumn(2).setPreferredWidth(10);
i am getting this ERROR --->>
Exception in thread "main" java.lang.IllegalArgumentException: Identifier not found
      at javax.swing.table.DefaultTableColumnModel.getColumnIndex(Unknown Source)
      at javax.swing.JTable.getColumn(Unknown Source)
      at browseSelect.main(browseSelect.java:113)

JTable table = new JTable(browseList, headersV);
               JScrollPane scrolls = new JScrollPane(table,
            		   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            		      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
               table.getColumn(2).setPreferredWidth(10);

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image


look here:
http://www.exampledepot.com/egs/javax.swing.table/ColSize.html

int rows = 3;
int cols = 3;
JTable table = new JTable(rows, cols);

// Disable auto resizing
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// Set the first visible column to 100 pixels wide
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
int width = 100;
col.setPreferredWidth(width);

Open in new window

But befire you do anything - popuate your table and look at it.
Why do you want to chaneg column size?
In majority of tables you dont need to do it
As you see to do it - you need to disable autoaddjustment - so you'll take a lot of unneccassry responsibilities upon yourself - let
swing do its job - it ill in most case do a better job
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial