By Alvin Alexander. Last updated: June 4, 2016
Here is a quick example of how to set JTable column widths. I don't know if this is a perfect solution, but it is one possible solution:
for (int i=0; i<numColumnsInTableModel; i++) { TableColumn column = myJTable.getColumnModel().getColumn(i); if (i==0) column.setPreferredWidth(10); if (i==1) column.setPreferredWidth(50); if (i==2) column.setPreferredWidth(50); if (i==3) column.setPreferredWidth(10); if (i==4) column.setPreferredWidth(100); if (i==5) column.setPreferredWidth(10); if (i==6) column.setPreferredWidth(10); if (i==7) column.setPreferredWidth(10); if (i==8) column.setPreferredWidth(10); if (i==9) column.setPreferredWidth(10); }
The basic implementation is this:
- For each column in your JTable table model (TableModel) ...
- Get a
TableColumn
reference from theJTable
. - Set the preferred width on that column.
Note that the preferred width values are relative values, not absolute positioning using pixels or anything like that.