We help IT Professionals succeed at work.

JTable column widths

allelopath asked
In a JTable, Is it possible to have some columns a fixed width and others not?
Watch Question

HonorGodSoftware Engineer

Yes.  For the fixed columns, set the minimum, and maximum, and preferred columns to the same value.
Software Engineer
For my Jython swing scripts, I include the following method, and call it specifying the table object, the column number, and the desired width.
  # Name: setColWidth()
  # Role: Used to fix the specified column # to the given width
  def setColWidth( self, table, colNum, width ) :
    col = table.getColumnModel().getColumn( colNum );
    col.setMinWidth( width );
    col.setMaxWidth( width );
    col.setPreferredWidth( width );

Open in new window


thanks this is really cool
 *	specify min, max and preferred width for a column
private void setColumnWidth(int columnNumber, int minWidth, int maxWidth) {

    TableColumn column = this.getColumnModel().getColumn(columnNumber);

    column.setMinWidth( minWidth );
    column.setMaxWidth( maxWidth );
    column.setPreferredWidth( minWidth );


Open in new window

HonorGodSoftware Engineer

Great.  I'm glad to hear that you found it helpful.

Thanks for the grade & points.

Good luck & have a great day.