On Fri, 7 Aug 2026 05:35:36 GMT, Prasanta Sadhukhan <[email protected]> 
wrote:

>> When JTable.setAutoResizeMode is called with JTable.AUTO_RESIZE_LAST_COLUMN, 
>>  it is supposed to adjust the delta width to the last column only when table 
>> itself changes width
>>  but before JDK-8234071 fix, AUTO_RESIZE_LAST_COLUMN, was behaving exactly 
>> as if user specified AUTO_RESIZE_ALL_COLUMNS
>>  so width of all columns of table gets adjusted. 
>>  
>>  JDK-8234071 fixes this issue by setting "resizingColumn" to last column 
>> when AUTO_RESIZE_LAST_COLUMN is specified so that only last column gets 
>> resized
>>  but the fix was wrongly instructing the JTable that the user is currently 
>> resizing the last column with the mouse
>>  so JTable started believing a header resize was active even during normal 
>> layout, window resizing or cell editing
>>  thus it caused side-effects like initial preferred column widths was 
>> ignored as seen in JDK-8375573
>>  and real mouse resizing of another column was conflicting with the “last 
>> column is resizing” state as mousePressed/Released uses "resizingColumn" to 
>> ensure a certain column is getting resized
>>  and editing is disturbed because JTable thought column resizing/layout 
>> activity is happening as seen in this particular issue.
>> 
>>  The issue is that a left click on an editable cell starts editing and then 
>> selects the cell. 
>>  Selecting the cell scrolls it into view, which revalidates the table and 
>> runs doLayout(). 
>>  Because the header now permanently reports a resizing column, the layout 
>> pass adjusts a column's preferred width 
>>  and fires TableColumnModelListener.columnMarginChanged(). 
>> JTable.columnMarginChanged() stops the active cell editor.
>>  
>>  Also, after a user drags the first column header divider, resizingColumn is 
>> reset to null in `BasicTableHeaderUI.mouseReleased` and
>>  JTable then syncs preferred widths from actual widths using 
>> `setWidthsFromPreferredWidths(true)`. 
>>  Later, when the dialog is resized, `setWidthsFromPreferredWidths(false)` 
>> runs again and all columns are recalculated so AUTO_RESIZE_LAST_COLUMN was 
>> not honoured
>>  
>>  Fix is made to handle AUTO_RESIZE_LAST_COLUMN in the layout code
>>  so the width distribution logic ensures that
>>   During normal window/dialog resize: change is made only to the last column.
>>   During initial layout: honors the user’s preferred widths, then let the 
>> last column absorbs extra space.
>>   During real header drag: allows the dragged column to resize, and uses the 
>> last column to compensate.
>>   and it doesn't hamper editing of any cell in last column when double-c...
>
> Prasanta Sadhukhan has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Prevent prefWidth from being reapplied

thank you for adding a boolean flag, it's the right approach.

I have a couple of questions related to adding/removing the columns, and a 
suggestion to explicitly code for semantic link between `TableColumn` default 
width and the magic number here.

src/java.desktop/share/classes/javax/swing/JTable.java line 3289:

> 3287:         for (int i = 0; i < columnModel.getColumnCount(); i++) {
> 3288:             TableColumn column = columnModel.getColumn(i);
> 3289:             if (column.getPreferredWidth() != 75 && column.getWidth() 
> == 75) {

Another suggestion is to maybe create a package protected

`static final int DEFAULT_WIDTH = 75;`

in the `TableColumn` class for use in constructors, and reference it from here?
this way it clearly establishes the relationships between related paths.

or, if you do not want to touch the `TableColumn` class, at least add a comment 
here to explain where the constant 75 came from.

src/java.desktop/share/classes/javax/swing/JTable.java line 3859:

> 3857:             throw new IllegalArgumentException("Cannot set a null 
> ColumnModel");
> 3858:         }
> 3859:         columnWidthsInitialized = false;

can you add a test for this case?

src/java.desktop/share/classes/javax/swing/JTable.java line 4673:

> 4671:      */
> 4672:     public void columnAdded(TableColumnModelEvent e) {
> 4673:         columnWidthsInitialized = false;

are you sure?

shouldn't it resize the _existing_ columns using their _current_ widths, but 
use the _preferred_ width of the newly added column?

src/java.desktop/share/classes/javax/swing/JTable.java line 4690:

> 4688:      */
> 4689:     public void columnRemoved(TableColumnModelEvent e) {
> 4690:         columnWidthsInitialized = false;

similarly here, are you sure?

if a column gets removed, it should probably not use the preferred widths, but 
use the existing widths (since the user might have adjusted the table to their 
liking, and we should always try to remember the user's choice).

-------------

PR Review: https://git.openjdk.org/jdk/pull/31704#pullrequestreview-4899514989
PR Review Comment: https://git.openjdk.org/jdk/pull/31704#discussion_r3752052522
PR Review Comment: https://git.openjdk.org/jdk/pull/31704#discussion_r3751976231
PR Review Comment: https://git.openjdk.org/jdk/pull/31704#discussion_r3751995964
PR Review Comment: https://git.openjdk.org/jdk/pull/31704#discussion_r3752005750

Reply via email to