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

时间:2011-06-01 17:42:18

标签: events swing jcombobox

修改
Downvoter,这怎么回事?我提供了该问题的可运行示例代码。如果它对你有用,请告诉我或指出不清楚的地方。

您好,
  在下面的代码JComboBox中只有一个JFrame,当鼠标进入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
}

谢谢!

3 个答案:

答案 0 :(得分:7)

使用Netbeans GUI编辑器时,downvoter可能会受到冒犯。我自己并不喜欢它,但是如果你发现你可以用它来保持复杂的gui,那么欢迎你使用它。我个人很讨厌它,因为各种非常烦人的错误只会在你试图编辑表单时显示出来并且它会悄然失去你的布局和组件设置。但这不是重点。

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

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

JComboBox实际上是一个复合组件,其中包含JTextField,JButton和JList,因此当鼠标事件真正进入内部JTextField时,您将ActionListener添加到包装组件。

答案 1 :(得分:0)

不要忘记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);
}

请访问swing mouse listeners being intercepted by child components了解详情。

答案 2 :(得分:0)

“我的目标是当单击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
   }
 });
}