开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜