United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4323107 : JList: two ListSelection events if using mouse (one if via keybd navigation)

Details
Type:
Bug
Submit Date:
2000-03-19
Status:
Closed
Updated Date:
2001-07-12
Project Name:
JDK
Resolved Date:
2001-07-12
Component:
client-libs
OS:
windows_98,windows_nt
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Not an Issue
Affected Versions:
1.2.1,1.3.0
Fixed Versions:

Related Reports

Sub Tasks

Description

Name: krT82822			Date: 03/18/2000


/*
1.3.0rc2-W
(see also 4298197 -- and 4134544 <<<--- )
orig. synopsis:  "Clicking on a line in a JList with the mouse gives double events"

Selecting a line in a JList using the mouse gives duplicate events, whereas
moving the cursor with the arrow keys give single events.
In the following, clicking with the mouse on a line (say 'two') results in two
identical lines being printed.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class JListBug102390 extends JFrame {
   
        public JListBug102390() {
        super( "Double selection bug" );
        Container c = getContentPane();
                String[] items = { "one", "two", "three", "four", "five" };
                final JList list = new JList(items);
                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                c.add(list);

           list.addListSelectionListener(
                new ListSelectionListener() {
                        public void valueChanged (ListSelectionEvent e) {
                                System.out.println(list.getSelectedValue());
                                }
                        }
           );

                pack();
        }

        public static void main(String[] args) {
                JListBug102390 f = new JListBug102390();
                f.setLocation(300,300);
                f.setVisible(true);
        }
}

(Review ID: 102390) 
======================================================================

                                    

Comments
WORK AROUND



Name: krT82822			Date: 03/18/2000


Monitor the value and check whether it has changed
======================================================================
                                     
2004-06-11
EVALUATION

We should use property DefaultListSelectionModel.isAdjusting to
prevent generation extra list selection events. We should check
JList.isValueIsAdjusting() value in JList's internal class
ListSelectionHandler.valueChanged() before we fire this event
with JList.fireSelectionValueChanged().

###@###.### 2000-06-05

The first event is sent while the mouse is down to indicate in the process of changing the selection (isAdjusting returns true), the second event is sent when the process has completed (isAdjusting == false). Because the keyboard operation is seen as atomic (we can't tell before hand how many key pressed events we'll see) you only get one event from the keyboard. If this is important to you, you can ignore events that have isAdjusting == true.
scott.violet@eng 2001-07-12
                                     
2001-07-12
SUGGESTED FIX


------- JList.java -------
*** /tmp/d0WXvIW	Mon Jun  5 18:23:49 2000
--- JList.java	Mon Jun  5 18:23:14 2000
***************
*** 959,967 ****
      private class ListSelectionHandler implements ListSelectionListener, Serializable 
      {
  	public void valueChanged(ListSelectionEvent e) {
! 	    fireSelectionValueChanged(e.getFirstIndex(),
! 				      e.getLastIndex(),
! 				      e.getValueIsAdjusting());
  	}
      }
  	
--- 959,969 ----
      private class ListSelectionHandler implements ListSelectionListener, Serializable 
      {
  	public void valueChanged(ListSelectionEvent e) {
!             if (!getValueIsAdjusting()) {
!                 fireSelectionValueChanged(e.getFirstIndex(),
!                                           e.getLastIndex(),
!                                           e.getValueIsAdjusting());
!             }
  	}
      }
  	

###@###.### 2000-06-05
                                     
2000-06-05



Hardware and Software, Engineered to Work Together