简体   繁体   English

如何在DefaultListModel中添加侦听器或双击元素以打开新的Jframe?

[英]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");

I want to double clicks "adfsdf" then open a new JFrame,how to handle? 我想双击“ adfsdf”,然后打开一个新的JFrame,如何处理?

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. 您可以将MouseListener添加到JList ,这将检查它是否是双击事件,并且该双击是在列表项上进行的。 Then, in your mouse listener, you'll open the JFrame . 然后,在鼠标侦听器中,打开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. ListAction类使您可以将Action添加到对JButton所做的列表中。 The Action will be invoked on a mouse double click or when using Enter from the key board. 双击鼠标或在键盘上使用Enter时,将调用该动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM