afsac
asked on
Jlist action question
I'm working on some code that uses jlist. It is working
fine except my action runs on mouse down and then again
on mouse up. I just want one action for a single mouse
click (up and down). Can anybody help?
Here is the code i'm using:
listModel = new DefaultListModel();
for (int i=0; i <= InData.size() -1; i++)
listModel.addElement(InDat a.elementA t(i));
list = new JList(listModel);
list.setSelectionModeListS electionMo del.SINGLE _SELECTION );
list.addListSelectionListe ner(this);
public void valueChanged(ListSelection Event e) {
Object xx=list.getSelectedValue() ;
jTextArea1.append(xx.toStr ing()+ "\n");
}
fine except my action runs on mouse down and then again
on mouse up. I just want one action for a single mouse
click (up and down). Can anybody help?
Here is the code i'm using:
listModel = new DefaultListModel();
for (int i=0; i <= InData.size() -1; i++)
listModel.addElement(InDat
list = new JList(listModel);
list.setSelectionModeListS
list.addListSelectionListe
public void valueChanged(ListSelection
Object xx=list.getSelectedValue()
jTextArea1.append(xx.toStr
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or use a MouseListener, and react to mouseClicked events.
ASKER
Fantastic! Works like a charm. I see from your code that
the solution is:
if (e.getValueIsAdjusting() == false) {
....
}
If you have time, could you give a brief explanation of
what this is, how in the world would I have ever found
this on my own, etc.
Thank you!
AFSAC
the solution is:
if (e.getValueIsAdjusting() == false) {
....
}
If you have time, could you give a brief explanation of
what this is, how in the world would I have ever found
this on my own, etc.
Thank you!
AFSAC
Straight out of the Java Tutorial section on using JList:
http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
No matter which selection mode your list uses, the list fires list selection events whenever the selection changes. You can process these events by adding a list selection listener to the list with the addListSelectionListener method. A list selection listener must implement one method: valueChanged. Here's the valueChanged method for the listener in SplitPaneDemo:
public void valueChanged(ListSelection Event e) {
if (e.getValueIsAdjusting())
return;
JList theList = (JList)e.getSource();
if (theList.isSelectionEmpty( )) {
picture.setIcon(null);
} else {
int index = theList.getSelectedIndex() ;
ImageIcon newImage = new ImageIcon("images/" + (String)imageList.elementA t(index));
picture.setIcon(newImage);
picture.setPreferredSize(n ew Dimension(newImage.getIcon Width(), newImage.getIconHeight())) ;
picture.revalidate();
}
}
Many list selection events can be generated from a single user action such as a mouse click. The getValueIsAdjusting method returns true if the user is still manipulating the selection. This particular program is interested only in the final result of the user's action, so the valueChanged method updates the image only if getValueIsAdjusting returns false.
:-)
Jim
http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
No matter which selection mode your list uses, the list fires list selection events whenever the selection changes. You can process these events by adding a list selection listener to the list with the addListSelectionListener method. A list selection listener must implement one method: valueChanged. Here's the valueChanged method for the listener in SplitPaneDemo:
public void valueChanged(ListSelection
if (e.getValueIsAdjusting())
return;
JList theList = (JList)e.getSource();
if (theList.isSelectionEmpty(
picture.setIcon(null);
} else {
int index = theList.getSelectedIndex()
ImageIcon newImage = new ImageIcon("images/" + (String)imageList.elementA
picture.setIcon(newImage);
picture.setPreferredSize(n
picture.revalidate();
}
}
Many list selection events can be generated from a single user action such as a mouse click. The getValueIsAdjusting method returns true if the user is still manipulating the selection. This particular program is interested only in the final result of the user's action, so the valueChanged method updates the image only if getValueIsAdjusting returns false.
:-)
Jim