JDK-4758217 : REGRESSION: Invalid indices returned by getSelectedIndices
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_9
  • CPU: sparc
  • Submitted: 2002-10-04
  • Updated: 2002-12-11
  • Resolved: 2002-12-11
Related Reports
Relates :  
Relates :  
Description

Name: gm110360			Date: 10/04/2002


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)


FULL OPERATING SYSTEM VERSION :
SunOS brooke 5.9 Generic_112233-01 sun4u sparc SUNW,Ultra-1




ADDITIONAL OPERATING SYSTEMS :
 

Linux gescher 2.4.19-PMC-SMP #3 SMP Mon Sep 9 22:33:11 CEST 2002 i686 unknown


A DESCRIPTION OF THE PROBLEM :
"Invalid" indices - that is, indices that I didn't add - are returned by getSelectedIndices() depending on the order setModel and addListSelectionListener are executed.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile and run the program below - the output is 0|1, even though the only index that was selected is 0.
2. Comment out the first line with setModel; uncomment the second line with setModel, compile and run the program again - the output is 0|

EXPECTED VERSUS ACTUAL BEHAVIOR :
The output should be the same (preferably 0|) in both cases.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class Foo {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final DefaultListModel model = new DefaultListModel();
        final JList list = new JList();
        ListDataListener ldl = new ListDataListener() {
            public void contentsChanged(ListDataEvent e) { }
            public void intervalRemoved(ListDataEvent e) { }
            public void intervalAdded(ListDataEvent e)  {
                if (model.getSize() != 0 && list.isSelectionEmpty()) {
                    list.setSelectedIndex(0);
                }
            }
        };
        list.setModel(model);
        model.addListDataListener(ldl);
        //list.setModel(model);
        model.add(0, "thou");
        int[] indices = list.getSelectedIndices();
        for(int i = 0; i < indices.length; i++)  {
            System.out.print(indices[i] + "|");
        }
        System.out.println();
        frame.getContentPane().add(list, BorderLayout.CENTER);
        frame.setSize(500,500);
        frame.setVisible(true);
     }
}
---------- END SOURCE ----------

Release Regression From : 1.3.1_05
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 165368) 
======================================================================

Comments
EVALUATION Name: apR10133 Date: 10/08/2002 JList.setModel() updates the LAF ListDataListener via PCE. This listener is responsible for sync of the selection model with a corresponding change in the data model and updates the selection indices. On the other hand the application adds its own ListDataListener which updates the selection. So, we have two listeners that tries to update the selection indices and the result depends on sequence of listeners being called. The dumped stacks for both calls that updates selection model are below. For LAF listener: java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1071) at javax.swing.DefaultListSelectionModel.insertIndexInterval(DefaultListSelectionModel.java:504) at javax.swing.plaf.basic.BasicListUI$ListDataHandler.intervalAdded(BasicListUI.java:1540) at javax.swing.AbstractListModel.fireIntervalAdded(AbstractListModel.java:130) at javax.swing.DefaultListModel.add(DefaultListModel.java:462) at bug4758217.init(bug4758217.java:39) at bug4758217.main(bug4758217.java:70) For application listener: java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1071) at javax.swing.DefaultListSelectionModel.set(DefaultListSelectionModel.java:273) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:397) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:412) at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:436) at javax.swing.JList.setSelectedIndex(JList.java:1733) at bug4758217$1.intervalAdded(bug4758217.java:30) at javax.swing.AbstractListModel.fireIntervalAdded(AbstractListModel.java:130) at javax.swing.DefaultListModel.add(DefaultListModel.java:462) at bug4758217.init(bug4758217.java:40) at bug4758217.main(bug4758217.java:69) If the DefaultListSelectionModel.setSelectionInterval() called before the DefaultListSelectionModel.insertIndexInterval() we get the inconsistent selected indices. ###@###.### ====================================================================== Name: apR10133 Date: 12/11/2002 We recommend using an invokeLater to update the selection. That way you will know that we have updated the state between the two models. Additionally there is a bug against beans (#4178930) to add ordering to event listeners so that Swing's listeners could be updated first, then application listeners. ###@###.### Close because: not a bug ======================================================================
24-08-2004

WORK AROUND Name: apR10133 Date: 10/08/2002 Set model after the adding of listener. ###@###.### ======================================================================
24-08-2004