United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-8005019 JTable passes row index instead of length when inserts selection interval
JDK-8005019 : JTable passes row index instead of length when inserts selection interval

Details
Type:
Bug
Submit Date:
2012-12-13
Status:
Resolved
Updated Date:
2013-07-28
Project Name:
JDK
Resolved Date:
2013-01-09
Component:
client-libs
OS:
windows_7
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6u31
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:
Backport:

Sub Tasks

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"});
    }
------------------------------------------------------
                                     
2012-12-14
URL:   http://hg.openjdk.java.net/jdk8/awt/jdk/rev/d54922883f4c
User:  alexsch
Date:  2013-01-09 12:58:19 +0000

                                     
2013-01-09
URL:   http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/d54922883f4c
User:  lana
Date:  2013-01-15 19:00:22 +0000

                                     
2013-01-15



Hardware and Software, Engineered to Work Together