2011-06-01 77 views
6

编辑
Downvoter,这是一个坏问题?我提供了可运行的示例代码。如果它适合你,请让我知道或指出什么不清楚。JComboBox焦点和鼠标单击事件不起作用

您好,
在低于该具有在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 
} 

谢谢!

回答

7

可能downvoter冒用您使用Netbeans GUI编辑器。我不喜欢它,但是如果你发现你实际上可以维持一个复杂的GUI,那么你可以使用它。我个人讨厌它,因为各种非常烦人的错误只会在您尝试编辑表单时显示出来,并且会悄然失去您的布局和组件设置。但那不是重点。

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

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

JComboBox可真正与一个JTextField,一个JButton,并JList的埋在它里面的复合成分,所以你添加的ActionListener到包装组件,当鼠标事件真的要去内部的JTextField。

+0

那么使用Netbeans是一个愚蠢的理由被冒犯。无论如何,非常感谢你解释添加鼠标监听器的正确位置。奇迹般有效! – 2011-06-02 16:33:05

+1

:D就像喝醉酒的人一样,憎恨者总会找到理由。 – enthusiasticgeek 2012-10-18 21:11:20

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了解更多详情。