简体   繁体   English

Java Swing JComboBox,mouseListener不起作用

[英]Java Swing JComboBox, mouseListener not working

I have a JComboBox that, when clicked, should update the contents of a JLabel. 我有一个JComboBox,当单击它时,应该更新JLabel的内容。 I'm currently using a mouseListener to detect when the user clicks on the JComboBox like so: 我目前正在使用mouseListener来检测用户何时单击JComboBox,如下所示:

myComboBox.getEditor().getEditorComponent().addMouseListener(
            new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            updateMyJLabel(evt);
            }
        });

I have no trouble actually updating the JLabel outside of this snippet. 实际上,在此代码段之外更新JLabel完全没有问题。 I previously had it set up so that I could change the contents of the JComboBox and then click a JButton to update the JLabel, and it worked fine. 我之前已经进行了设置,以便可以更改JComboBox的内容,然后单击JButton更新JLabel,它可以正常工作。 However, it quickly became tedious to click the button every time I need to update the JLabel. 但是,每次我需要更新JLabel时单击按钮很快就变得很乏味。 But when I add myComboBox to the layout after using the above code, the code never actually executes. 但是,当我在使用上述代码后将myComboBox添加到布局中时,该代码实际上不会执行。 I also tried putting a print statement above the call to updateMyJLabel, but even that didn't do anything, the console was beautifully, frustratingly blank. 我还尝试在对updateMyJLabel的调用之前放置一条打印语句,但是即使那样也没有执行任何操作,该控制台还是很漂亮,令人沮丧地空白。

This is only my second day of attempting ui development, so sorry if this is a dumb question. 这只是我尝试进行ui开发的第二天,如果这是一个愚蠢的问题,对不起。 I read quite a number of other questions here on SO, and some people have said not to use a mouseListener on a JComboBox, others have said code like this worked perfectly for them, so I'm a bit confused as to why this isn't working. 我在SO上阅读了很多其他问题,有些人说不要在JComboBox上使用mouseListener,其他人说像这样的代码对他们来说效果很好,所以我对为什么这不是很困惑。工作。

Any suggestions and help are greatly appreciated. 任何建议和帮助,我们将不胜感激。

See JComboBox.addItemListener(ItemListener) . 请参见JComboBox.addItemListener(ItemListener) It works reliably on mouse or keyboard selection. 它在选择鼠标或键盘时可以可靠地工作。

Adding mouse listeners to JComboBox is always going to be a problem. JComboBox添加鼠标侦听器总是一个问题。 It tends to be implemented by PL&Fs as a container of other components. 它通常由PL&F实施为其他组件的容器。 (Of course a PL&F may choose to do something else, breaking lots of dodgy code.) Mouse events behave very peculiarly, bubbling up to the parent contain iff there are no mouse listeners on the current component. (当然,PL&F可能会选择执行其他操作,从而破坏了许多狡猾的代码。)鼠标事件的行为非常特殊,如果当前组件上没有鼠标侦听器,则冒泡到父对象。 Adding a mouse listener changes the behaviour of a component. 添加鼠标侦听器会更改组件的行为。

(I'd always put an @Override when attempting to override a method. Amazing how common and confusing it is to get it wrong. Also MouseAdapter is a bit nasty as you may use it as a MouseListener or MouseMotionListener .) (我会永远把一个@Override试图重写方法时,令人惊讶的是常见的,混乱是理解错误,另外MouseAdapter是有点讨厌,你可以使用它作为一个MouseListenerMouseMotionListener 。)

I'm not entirely sure what you are trying to do. 我不确定您要做什么。 Probably adding a listener to the combo box model would make most sense. 可能在组合框模型中添加侦听器最有意义。 If you want to reliably add a mouse listener, you'd probably need to go for a "glass pane" hack. 如果要可靠地添加鼠标侦听器,则可能需要进行“玻璃窗格”破解。

here is an example for you 这是给你的一个例子

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Demo Frame/SuRu");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        final JLabel jLabel = new JLabel();
        final JComboBox box = new JComboBox();
        box.addItem("");
        box.addItem("Item 1");
        box.addItem("Item 2");
        box.addItem("Item 3");
        box.addItem("Item 4");
        box.addItem("Item 5");
        box.addItem("Item 6");
        box.addItem("Item 7");
        box.addItem("Item 8");
        box.addItem("Item 9");
        box.addItem("Item 10");
        contentPane.add(new JLabel("Select Here: "));
        contentPane.add(box);
        contentPane.add(new JLabel("Seleced Item: "));
        contentPane.add(jLabel);
        box.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent arg0) {
                jLabel.setText(box.getSelectedItem().toString());
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(200, 200, 400, 100);
        frame.setVisible(true);
    }
}

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

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