How to add Listener or double clicks element in DefaultListModel to open a new Jframe?
dlm = new DefaultListModel();
jl = new JList(dlm);
dlm.addElement("adfsdf");
开发者_JAVA技巧I want to double clicks "adfsdf" then open a new JFrame,how to handle?
You can add a MouseListener
to your JList
which will check it's a double-click event, and that this double-clicks occurs over a list item. Then, in your mouse listener, you'll open the JFrame
.
Something like (haven't tested, but see the point) :
JList list = new JList(dataModel);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
if ((String)list.getSelectedValue()).equals("adfsdf") {
// do stuff
}
}
}
};
list.addMouseListener(mouseListener);
The ListAction class allows you to add an Action to a list list you do for a JButton. The Action will be invoked on a mouse double click or when using Enter from the key board.
精彩评论