TableColumn MinWidth not changing

40 Views Asked by At

I have a JTable where I want to dynamically change the width of one column to a set value and back again based on user input. However, I cannot change the column's MinWidth, Width and MaxWidth properties.

// Declared within the class so it persists during run time
TableColumn firstColumn;

Within the Initialize method that setups up the console for display

JTable cellTable = new JTable(cellDataModel);
cellTable.setRowSorter(myTableRowSorter);
cellTable.setDefaultRenderer(Object.class, cellRender);
cellTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  // Tried both off and on, no difference
TableColumnModel tcm = cellTable.getColumnModel();
tcm.getColumn(0).setPreferredWidth(120);
tcm.getColumn(0).setMinWidth(120);  // This correctly sets the column's width to 120.
cellTable.setFillsViewportHeight(true);
firstColumn = tcm.getColumn(0);

chkHide is a JCheckBox used to resize the first column.

private void chckHideClicked(ActionEvent ae) {
    int width = chkHide.isSelected() ? 80:120;
    System.out.println("Set width to " + Integer.toString(width));
    firstColumn.setMinWidth(width);
    firstColumn.setPreferredWidth(width);
    firstColumn.setMaxWidth(width);
    System.out.println("My width is now " + Integer.toString(firstColumn.getWidth()) + 
            " and minWidth is now " + Integer.toString(firstColumn.getMinWidth()));
    updateData();  // Forces an update of the table model and table
}

When I run the code, the first check of the checkbox reduces the width of the first column from 120 to 80 as expected. However, subsequent clicks of the checkbox have no effect. Using the debugger, I've confirmed that setMinWidth(120) has no effect on MinWidth. The console output is:

Set width to 80
My width is now 80 and minWidth is now 80
Set width to 120
My width is now 80 and minWidth is now 80
Set width to 80
My width is now 80 and minWidth is now 80
Set width to 120
My width is now 80 and minWidth is now 80

Neither the table model nor the cell renderer have any code that explicitly changes the column width. Why won't the column width update width values?

0

There are 0 best solutions below