JComboBox focus and mouse click events not working
Asked Answered
C

3

8

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!

Crinkle answered 1/6, 2011 at 17:42 Comment(0)
O
12

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.

Oglesby answered 2/6, 2011 at 16:0 Comment(2)
Well using Netbeans is a silly reason to be offended. Anyways, thank you very much for explaining the right place to add the mouse listener. Works like a charm!Crinkle
:D Just like drunkards who need a reason to drink, haters will always find a reason.Sheepdog
S
1

"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
   }
 });
}
Selfridge answered 22/8, 2019 at 5:26 Comment(1)
Can you tell me what is borderless?Alicyclic
K
0

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.

Kitti answered 22/2, 2017 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.