wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList" Watch "JList" New topic
Author

JList

Frances Fang
Greenhorn

Joined: Dec 01, 2000
Posts: 17
Hi,all,
I am using a JList and add a ListSelectionListener.But every time I click the item in the JList once,it fires two ListSelectionEvent because the valueChanged() is invoked twice.Anybody know how to fix this?
Thank you in advance and May you have a merry Christmas!
Frances
pat patel
Greenhorn

Joined: Dec 13, 2000
Posts: 2
Well I experienced a similar problem, so I removed the ListSelectionListener from my JList, instead, I added a JButton to get what I needed, in the actionPerformed() I called the getSelectedIndex() of the JList, to get the value. I don't know if this will help, because I don't know exactly what you are trying to do. but here is a code snipett to help.
JList jl = new JList();
c.add(jl);
JButton but = new JButton("submit");
but.addActionListener
etc...
// skip to action performed method.
public void actionPerformed(actionEvent e{
variable = JList.getSelectedIndex();
// do stuff
Hope it is of some help
Frances Fang
Greenhorn

Joined: Dec 01, 2000
Posts: 17
Hi, Pat Patal,
I just got back from out-of-town.Sorry I am late and thank you for your help! I am working on it now.

Frances
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
The 2 callbacks are the result of the mouse interaction.
1 callback for mouse down
1 callback for mouse up
If you are only interested in the mouse up callback then your handler can just check the event flag isAdjusting:
true for mouse down, false for mouse up
public void valueChanged(ListSelectionEvent evt)
{
if( !ev.isAdjusting() )
{
// perform some work here.
}
}
The above solution has a problem with the 1.2 version of Swing if your list allows multiple selections (CTRL+click). The mouse up for this case doesn't generate therefore your routine will never perform any work. But since you had the multiple callback problem your list is probably SINGLE_SELECTION.
Manfred.
Frances Fang
Greenhorn

Joined: Dec 01, 2000
Posts: 17
Hi,Manfred,
I finally set a boolean flag to control it. But your ev.getValueIsAdjusting() is a better,neat idea. I tried and succeeded. Thank you very much!
Frances
 
 
subject: JList
 
Similar Threads
Regarding JList and DefaultListModel.
Count items in JList
How to add a String array to JList?
JList + Vector
how to hide popup ?