JDK-7027139 : getFirstIndex() does not return the first index that has changed
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6u24
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2011-03-13
  • Updated: 2014-10-24
  • Resolved: 2012-03-27
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 7 JDK 8
7u6Fixed 8 b32Fixed
Description
FULL PRODUCT VERSION :
J2SEE 6

ADDITIONAL OS VERSION INFORMATION :
Windows XP

A DESCRIPTION OF THE PROBLEM :
An api that worked fine in JDK 1.4  is not working corrctly in 1.6:
The method getFirstIndex() on ListSelectionEvent. that has been working fine on JDK 1.4 is no longer working as expected in 1.6

The following code when executed  in 1.4 return the row whose selection status was changed.

        if (e.getSource() == orderTable.getSelectionModel() &&
            e.getFirstIndex() >= 0 && e.getValueIsAdjusting() == false)
        {
           .....
         .......
        }

In JDK i.6 if the event is triggered programatically likke by aTable.selectAll(),  e.getFirstIndex()  returns a -1 instead of the row that was selected.

REGRESSION.  Last worked in version 1.4.2

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Assign a  selection model to a jtable like
    ListSelectionModel selectionModel = orderTable.getSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.
                                    MULTIPLE_INTERVAL_SELECTION);
//    jScrollPane1.getViewport().add(orderTable, null);
//        orderTable.setOpaque(false);
    selectionModel.addListSelectionListener(new ListSelectionListener()
    {
      public void valueChanged(ListSelectionEvent e)
      {
        if (e.getSource() == orderTable.getSelectionModel() &&
            e.getFirstIndex() >= 0 && e.getValueIsAdjusting() == false)
        {
          setOrderTableButtonState();
          setSkuForPendingOrderDataVector(
              convertSkuDetailForPendingAllocationOrderListToVector(
              getSkuDetailForSelectedSplitOrders()));

        }

      }
    });



and  ten programatically call selectAl() on the table.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
 e.getFirstIndex() returns0
ACTUAL -
 e.getFirstIndex() e.getFirstIndex() returns -1

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
                                    MULTIPLE_INTERVAL_SELECTION);
//    jScrollPane1.getViewport().add(orderTable, null);
//        orderTable.setOpaque(false);
    selectionModel.addListSelectionListener(new ListSelectionListener()
    {
      public void valueChanged(ListSelectionEvent e)
      {
        if (e.getSource() == orderTable.getSelectionModel() &&
            e.getFirstIndex() >= 0 && e.getValueIsAdjusting() == false)
        {
          setOrderTableButtonState();
          setSkuForPendingOrderDataVector(
              convertSkuDetailForPendingAllocationOrderListToVector(
              getSkuDetailForSelectedSplitOrders()));

        }

      }
    });


 orderTable.selectAll();

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Dont use  e.getFirstIndex() >= 0

Comments
EVALUATION The problem exists from jdk 1.5. because the javax.swing.JTable#selectAll method was cardinally changed. The simplest fix is to deny set negative values to the firstAdjustedIndex and lastAdjustedIndex fields in the DefaultListSelectionModel#markAsDirty method. Those fields are used only for value changed notification (see fireValueChanged methods) and shouldn't be negative at all, because there are no rows with negative indexes.
11-03-2012

EVALUATION regression case - so targeted to 7u6
14-02-2012

EVALUATION A small regression from 1.4 to 1.6 is not treated as a P3, downgraded
16-05-2011