2011-06-01 81 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瞭解更多詳情。