Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4323107
Votes 19
Synopsis JList: two ListSelection events if using mouse (one if via keybd navigation)
Category java:classes_swing
Reported Against 1.2.1 , kestrel-rc2
Release Fixed
State 11-Closed, Not a Defect, bug
Priority: 3-Medium
Related Bugs
Submit Date 19-MAR-2000
Description




/*
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) 
======================================================================
Work Around




Monitor the value and check whether it has changed
======================================================================
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().

  xxxxx@xxxxx   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.
  xxxxx@xxxxx   2001-07-12
Comments
  
  Include a link with my name & email   

Submitted On 01-OCT-2000
killj0y
Please update 'Reported Against' to include JDK 1.3.

I am not sure why this is not fixed, when the fix is listed 
in the evaluation?  Does it take that long to type in the 
fix? &lt;sigh&gt;


Submitted On 06-JAN-2001
widebluesky
// It appears that there is one ListSelectionEvent on mouse
press and another one on mouse release.
// This program illustrate this point.

/* The main window has to be resized for the viewResult list
to be visible.
   
   Now you can observe that when pressing mouse button on
First element for example
   the viewResult list print One Two Three and when you
release mouse button, viewResult
   changes its content and print Four Five Six.
   
   There are two element in the actionList for the
experience to be possibly repeated */

// That's all, I hope it could help.

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


class BugReport extends JFrame {

    static boolean flipflop = true;

    String [] listData = {&quot;First element&quot;, &quot;Second
element&quot;};

    final String [] stringToPrintOne = {&quot;One&quot;, &quot;Two&quot;,
&quot;Three&quot;};
    final String [] stringToPrintTwo = {&quot;Four&quot;, &quot;Five&quot;,
&quot;Six&quot;};

    JList actionList = new JList (listData);
    
    JList viewResult = new JList ();

    public BugReport (String title) {

	setTitle (title);

	getContentPane ().add (actionList, &quot;North&quot;);
	getContentPane ().add (viewResult, &quot;Center&quot;);

	actionList.addListSelectionListener (new
ListSelectionListener () {

		public void valueChanged (ListSelectionEvent e) {
		    
		    if (flipflop)
			viewResult.setListData (stringToPrintOne);
		    else
			viewResult.setListData (stringToPrintTwo);
		    
		    flipflop = !flipflop;
		    
		}
		
	    });

	pack ();
	show ();

    }
	
    public static void main (String [] args) {
	
	new BugReport (&quot;Bug report : two ListSelectionEvent
generated on mouse click&quot;);

    }

}


Submitted On 09-JAN-2001
s_nonkichi
This bug also appears in JTable like below.
.....
  final JTable table = new JTable(convertedRows, 
sessionSchool.getColumnMeta());
  ListSelectionModel listSelectionModel = 
table.getSelectionModel();
  listSelectionModel.setSelectionMode
(ListSelectionModel.SINGLE_SELECTION);
  table.getSelectionModel().addListSelectionListener(new 
ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel model = (ListSelectionModel)
e.getSource();
        System.out.println(table.getModel().getValueAt
(model.getMinSelectionIndex(), 1).toString());
       }
    }
  );
.....


Submitted On 19-JAN-2001
Pushps
In case of JTable navigation across rows , a Single Event 
will be fired if the navigation is done thru Keyboard , and 
multiple events will be fired if the navigation is done 
thru mouse. 

So its but Obvious that one of the Events is fired due to 
Deselection of a row and the other event is fired due to 
selection of a new row.

The workaround can be written inside the Event Handler by 
checking the boolean value of &quot;isValueAdjusting&quot;. If its 
true the actions written inside the event Handler will be 
executed twice and if its false  it would be executed only 
once.





PLEASE NOTE: JDK6 is formerly known as Project Mustang