JComboBox 焦点和鼠标单击事件不起作用

发布于 2024-11-11 07:59:34 字数 4242 浏览 4 评论 0原文

编辑
投反对票的人,这是一个糟糕的问题吗?我提供了该问题的可运行示例代码。如果它对您有用,请告诉我或指出不清楚的地方。

你好,
在下面的代码中,JFrame 中有一个 JComboBox,当鼠标进入 JComboBox 或被单击或获得焦点时,我不会收到通知。但是,PopupMenuEvent 可以正常工作。

我做错了什么? (我的目标是在单击 JComboBox 的文本组件时收到警报)

public class TestJComboBox extends javax.swing.JFrame
{
    public TestJComboBox()
    {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jComboBox1.setEditable(true);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jComboBox1MouseEntered(evt);
            }
        });
        jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox1PopupMenuWillBecomeVisible(evt);
            }
        });
        jComboBox1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jComboBox1FocusGained(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(164, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jComboBox1FocusGained(java.awt.event.FocusEvent evt)
    {
        System.out.println("JComboBox Focus gained");
    }

    private void formMouseClicked(java.awt.event.MouseEvent evt)
    {
        System.out.println("Form clicked");
        jComboBox1.setFocusable(false);
        jComboBox1.setFocusable(true);
    }

    private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt)
    {
        System.out.println("JComboBox Click");
    }

    private void jComboBox1PopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt)
    {
        System.out.println("JComboBox Visible menu");
    }

    private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt)
    {
        System.out.println("Entered JComboBox");
    }

    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new TestJComboBox().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration
}

谢谢!

Edit
Downvoter, how is this a bad question? I have provided runnable example code of the issue. If it works for you please let me know or point out what is unclear.

Hello,
in the code below which has a single JComboBox in a JFrame, I am not notified when the mouse enters the JComboBox or is clicked or focus gained. However, the PopupMenuEvent works properly.

What am I doing wrong? (My goal is to be alerted when the text component of the JComboBox is clicked)

public class TestJComboBox extends javax.swing.JFrame
{
    public TestJComboBox()
    {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jComboBox1.setEditable(true);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jComboBox1MouseEntered(evt);
            }
        });
        jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox1PopupMenuWillBecomeVisible(evt);
            }
        });
        jComboBox1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jComboBox1FocusGained(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(164, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jComboBox1FocusGained(java.awt.event.FocusEvent evt)
    {
        System.out.println("JComboBox Focus gained");
    }

    private void formMouseClicked(java.awt.event.MouseEvent evt)
    {
        System.out.println("Form clicked");
        jComboBox1.setFocusable(false);
        jComboBox1.setFocusable(true);
    }

    private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt)
    {
        System.out.println("JComboBox Click");
    }

    private void jComboBox1PopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt)
    {
        System.out.println("JComboBox Visible menu");
    }

    private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt)
    {
        System.out.println("Entered JComboBox");
    }

    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new TestJComboBox().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山人契 2024-11-18 07:59:34

可能投反对票的人对您使用 Netbeans GUI 编辑器感到愤怒。我自己不喜欢它,但是如果您发现实际上可以用它维护一个复杂的 GUI,那么欢迎您使用它。我个人讨厌它,因为各种极其烦人的错误只有在您尝试编辑表单时才会出现,并且它会悄悄地丢失您的布局和组件设置。但这不是重点。

无论如何,您需要像这样添加 ActionListener:

jComboBox1.getEditor().getEditorComponent().addMouseListener(...);

JComboBox 实际上是一个复合组件,其中埋有 JTextField、JButton 和 JList,因此您将 ActionListener 添加到包装组件,而鼠标事件实际上会传递到内部JTextField。

Possibly the downvoter took offense at your use of Netbeans GUI editor. I don't like it myself, but you're welcome to use it if you find that you can actually maintain a complex gui with it. I personally hate it due to various extremely annoying bugs that only show themselves when you're trying to edit the form and it quietly loses your layout and component settings. But that's beside the point.

Anyway, you need to add your ActionListener like this:

jComboBox1.getEditor().getEditorComponent().addMouseListener(...);

JComboBox is really a composite component with a JTextField, JButton, and JList buried inside it, so you were adding the ActionListener to the wrapping component, when the mouse events are really going to the inner JTextField.

森林散布 2024-11-18 07:59:34

“我的目​​标是在单击 JComboBox 的文本组件时收到警报”

这可以通过将 FocusListener 添加到 JComboBox 的基础 JTextField 组件来实现,该组件将按预期响应 ComboBox 的焦点获得和失去。

Component component = comboBox.getEditor().getEditorComponent();  
if (component instanceof JTextField){
 JTextField txtFiled = (JTextField) borderless;
 txtFiled.addFocusListener(new FocusListener() 
 {
   public void focusGained(FocusEvent e) 
   {
     //To Do Focus Gained
   }
   public void focusLost(FocusEvent e) 
   {
     //To Do Focus Lost
   }
 });
}

"My goal is to be alerted when the text component of the JComboBox is clicked"

This can be achieved by adding FocusListener to the underlying JTextField Component of JComboBox that will respond as expected to gain and loss of focus for the ComboBox.

Component component = comboBox.getEditor().getEditorComponent();  
if (component instanceof JTextField){
 JTextField txtFiled = (JTextField) borderless;
 txtFiled.addFocusListener(new FocusListener() 
 {
   public void focusGained(FocusEvent e) 
   {
     //To Do Focus Gained
   }
   public void focusLost(FocusEvent e) 
   {
     //To Do Focus Lost
   }
 });
}
伤痕我心 2024-11-18 07:59:34

不要忘记,comboBox 实际上是一个容器。因此,如果您确实想要拥有所有鼠标事件,您应该将侦听器添加到它包含的所有组件中。

public void addMouseListener(final MouseListener mouseListener) {
    this.comboBox.addMouseListener(mouseListener);

    final Component[] components = this.comboBox.getComponents();
    for(final Component component : components) {
        component.addMouseListener(mouseListener);
    }
    this.comboBox.getEditor().getEditorComponent().addMouseListener(mouseListener);
}

请访问摆动鼠标侦听器被子组件拦截了解更多信息细节。

Don't forget that comboBox is actually a container. So if you really want to have all the mouse events you should add the listener to all the components it contains.

public void addMouseListener(final MouseListener mouseListener) {
    this.comboBox.addMouseListener(mouseListener);

    final Component[] components = this.comboBox.getComponents();
    for(final Component component : components) {
        component.addMouseListener(mouseListener);
    }
    this.comboBox.getEditor().getEditorComponent().addMouseListener(mouseListener);
}

Please visit swing mouse listeners being intercepted by child components for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文