Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6481195

ListSelectionListener indicates events on model.addElement after model.clear()

    XMLWordPrintable

    Details

      Description

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

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


      A DESCRIPTION OF THE PROBLEM :
      Under the following circumstances an added ListSelectionListener of a JList indicates ListSelectionEvents that should not be reported:

      - The JList uses a DefaultListModel object as model
      - The used DefaultListModel object contains data
      - The selcted itemIndex is set to 0
      - DefaultListModel.clear() is called

      If these preconditions are met in this order a ListSelectionEvent is reported on each call of DefaultListModel.addElement( Object obj ).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and execute the sample code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Output on stdOut:
      selectedIndex before clear(): -1 numberOfEvents: 0
      selectedIndex before clear(): 0 numberOfEvents: 2
      selectedIndex before clear(): 1 numberOfEvents: 2
      selectedIndex before clear(): 2 numberOfEvents: 2
      ACTUAL -
      Output on stdOut:
      selectedIndex before clear(): -1 numberOfEvents: 0
      selectedIndex before clear(): 0 numberOfEvents: 5
      selectedIndex before clear(): 1 numberOfEvents: 2
      selectedIndex before clear(): 2 numberOfEvents: 2

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.JList;
      import javax.swing.DefaultListModel;
      import javax.swing.event.ListSelectionEvent;
      import javax.swing.event.ListSelectionListener;

      public class BugDemo {

      public final static String[] DATA = new String[] { "one", "two", "three" };
      public static int numberOfEvents;

      public static void main( String[] args ) {
      JList list = new JList();
      list.addListSelectionListener( new ListSelectionListener() {
      public void valueChanged( ListSelectionEvent e ) {
      ++numberOfEvents;
      }
      });

      for( int i=-1; i<DATA.length; i++ ){
      DefaultListModel model = new DefaultListModel();
      list.setModel( model );
      addData( model, DATA );
      numberOfEvents = 0;
      list.setSelectedIndex( i );
      model.clear();
      addData( model, DATA );
      System.out.println( "selectedIndex before clear(): "+i+" numberOfEvents: "+numberOfEvents );
      }
      }

      private static void addData( DefaultListModel model, Object[] data ){
      for( int i=0; i<data.length; i++ ){
      model.addElement( data[i] );
      }
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Set the index manually to anything > 0 and set it back afterwards. Setting it to -1 does not work.

        Attachments

          Activity

            People

            Assignee:
            pbansal Pankaj Bansal
            Reporter:
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: