events for jlist

R

rmacnak

I have a JList of objects, I know how to get an event of an item is
selected, but how do i receive an event if an item is double clicked?

thanks in advance
 
K

Kurt M Peters

Try something like this:
jListFiles.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt){
JList list = (JList)evt.getSource();
if (evt.getClickCount()==2) goButtonClicked(evt);
}
});
 
R

Roedy Green

I have a JList of objects, I know how to get an event of an item is
selected, but how do i receive an event if an item is double clicked?

quoting from the JavaDoc

JList doesn't provide any special support for handling double or
triple (or N) mouse clicks however it's easy to handle them using a
MouseListener. Use the JList method locationToIndex() to determine
what cell was clicked. For example:

final JList list = new JList(dataModel);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
}
}
};
list.addMouseListener(mouseListener);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top