JDK-8005019 : JTable passes row index instead of length when inserts selection interval
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6u31
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-12-13
  • Updated: 2014-04-23
  • Resolved: 2013-01-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7 JDK 8
6u43Fixed 7u21Fixed 8 b73Fixed
Description
FULL PRODUCT VERSION :
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
This is a fairly straightforward bug where a row index is passed to a method that is expecting a length instead.

In JTable$SortManager.cacheSelection(RowSorterEvent sortEvent, ModelChange change) method, when:

modelSelection.insertIndexInterval(change.startModelIndex,
                                                       change.endModelIndex,
                                                       true);

The 2nd arg should be: change.endModelIndex - change.startModelIndex + 1

This can be verified with DefaultListSelectionModel.insertIndexInterval method.


REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
There's no workaround as the JTable$SortManager class is private final.
Comments
User's code that reproduces the issue: ------------------------------------------------------ @Test public void testSelectionWithFilterTable() { DefaultTableModel model = new DefaultTableModel(0, 1); // a model with 3 elements is the minimum to demonstrate // the bug int last = 2; for (int i = 0; i <= last; i++) { model.addRow(new Object[]{i}); } JTable table = new JTable(model); table.setAutoCreateRowSorter(true); // set selection at the end table.setRowSelectionInterval(last, last); // exclude rows based on identifier final RowFilter filter = new RowFilters.GeneralFilter() { List excludes = Arrays.asList(0); @Override protected boolean include( Entry<? extends Object, ? extends Object> entry, int index) { return !excludes.contains(entry.getIdentifier()); } }; ((DefaultRowSorter) table.getRowSorter()).setRowFilter(filter); // insertRow _before or at_ selected model index, such that // endIndex (in event) > 1 model.insertRow( 2, new Object[]{"x"}); } ------------------------------------------------------
14-12-2012