2011-06-01 9 views
6

Düzenleme
Downvoter, bu nasıl kötü bir sorudur? Sorunun runnable örnek kodunu sağladım. Eğer sizin için çalışıyorsa lütfen bana bildirin veya neyin belirsiz olduğunu belirtin. Fare JComboBox girdiğinde veya tıklandığında veya kazanılan odak zamanJComboBox odak ve fare tıklama etkinlikleri çalışmıyor

Merhaba, bir JFrame tek JComboBox sahip olduğu aşağıdaki kodu
, ben haberdar değilim. Ancak, PopupMenuEvent düzgün çalışıyor.

Neyi yanlış yapıyorum? (Hedefim, JComboBox'un metin bileşeni tıklandığında uyarılmaktır)

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 
} 

Teşekkürler!

cevap

7

Muhtemelen downvoter, Netbeans GUI düzenleyicisini kullandığınızda saldırıya uğradı. Kendimden hoşlanmıyorum, ancak onunla gerçekten karmaşık bir gui'yi sürdürdüğünüzü bulursanız, bunu kullanmaya davetlisiniz. Kendimi sadece formunu düzenlemeye çalıştığınızda kendini gösteren ve son derece rahatsız edici hatalardan dolayı nefret ediyorum ve düzeninizi ve bileşen ayarlarınızı sessizce kaybediyor. Ama bu noktanın yanında.

Neyse, bu gibi ActionListener eklemeniz gerekir:

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

JComboBox gerçekten içine gömülü bir JTextField, JButton ve JList ile kompozit bir bileşenidir, bu nedenle sarma bileşenine ActionListener ekleyerek edildi Fare olayları gerçekten iç JTextField'a gittiğinde.

+0

Netbeans'i kullanmak, rahatsız edilmenin saçma bir nedenidir. Her neyse, fare dinleyiciyi eklemek için doğru yeri açıkladığınız için çok teşekkür ederim. ÇALIŞIYOR bir cazibe gibi! –

+1

: D Bir içki içmeye ihtiyaç duyan sarhoşlar gibi, haters her zaman bir sebep bulur. – enthusiasticgeek

0

Bu comboBox aslında bir kap olduğunu unutmayın. Dolayısıyla, tüm fare olaylarına sahip olmak istiyorsanız, dinleyiciyi içerdiği tüm bileşenlere eklemelisiniz.

 

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); 
} 

Daha fazla ayrıntı için lütfen swing mouse listeners being intercepted by child components adresini ziyaret edin.