Double-Click.

I need to get the element selected from the JList when the user double click on the element inside JList. But how?Thanks.
[135 byte] By [coffee95a] at [2007-11-26 22:39:15]
# 1
Read and respond to your old postings. You where given the answer to this in your last posting.
camickra at 2007-7-10 11:52:00 > top of Java-index,Desktop,Core GUI APIs...
# 2
No, my last post was about Enter Key on JList.Now is about double click inside the JList.
coffee95a at 2007-7-10 11:52:00 > top of Java-index,Desktop,Core GUI APIs...
# 3

You should read swing tutorials about listeners first.

This is done very easy.

list.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

if (e.getClickCount() == 2) {

//your code

}

}

});

Andriuwkoa at 2007-7-10 11:52:00 > top of Java-index,Desktop,Core GUI APIs...
# 4
> No, my last post was about Enter Key on JList.> Now is about double click inside the JList. Like I said if you actually read the solution you will find the solution works for "Enter" key AND double click.Did you read the answer and try executing the code
camickra at 2007-7-10 11:52:00 > top of Java-index,Desktop,Core GUI APIs...
# 5

I think this might help you!!

System print out when you double click some item.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

public class AsciiChar_Listing {

String[] data = { "zero", "one", "two", "three", "four", "five" };

JList list = new JList(data);

public AsciiChar_Listing() {

Action displayAction = new AbstractAction() {

public void actionPerformed(ActionEvent e)

{

JList list = (JList)e.getSource();

System.out.println(list.getSelectedValue());

}

};

this.addAction(list, displayAction);

}

private static final KeyStroke ENTER = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

static void addAction(JList source, Action action) {

InputMap im = source.getInputMap();

im.put(ENTER, ENTER);

source.getActionMap().put(ENTER, action);

source.addMouseListener( new ActionMouseListener() );

}

static class ActionMouseListener extends MouseAdapter {

public void mouseClicked(MouseEvent e) {

if (e.getClickCount() == 2) {

JList list = (JList)e.getSource();

Action action = list.getActionMap().get(ENTER);

if (action != null) {

ActionEvent event = new ActionEvent(list,ActionEvent.ACTION_PERFORMED,"");

action.actionPerformed(event);

}

}

}

}

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) { }

JFrame frame = new JFrame();

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.getContentPane().add( new JScrollPane(new AsciiChar_Listing().list) );

frame.setSize(200, 300);

frame.setLocationRelativeTo( null );

frame.setVisible( true );

}

}

henyoa at 2007-7-10 11:52:00 > top of Java-index,Desktop,Core GUI APIs...
# 6
> I think this might help you!!I already gave the OP the link to my original posting of that code in his previous posting. That why I asked him if he bothered to actually read the posting.
camickra at 2007-7-10 11:52:00 > top of Java-index,Desktop,Core GUI APIs...