Question

I have a JComboBox that, when clicked, should update the contents of a JLabel. I'm currently using a mouseListener to detect when the user clicks on the JComboBox like so:

myComboBox.getEditor().getEditorComponent().addMouseListener(
            new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            updateMyJLabel(evt);
            }
        });

I have no trouble actually updating the JLabel outside of this snippet. I previously had it set up so that I could change the contents of the JComboBox and then click a JButton to update the JLabel, and it worked fine. However, it quickly became tedious to click the button every time I need to update the JLabel. But when I add myComboBox to the layout after using the above code, the code never actually executes. I also tried putting a print statement above the call to updateMyJLabel, but even that didn't do anything, the console was beautifully, frustratingly blank.

This is only my second day of attempting ui development, so sorry if this is a dumb question. I read quite a number of other questions here on SO, and some people have said not to use a mouseListener on a JComboBox, others have said code like this worked perfectly for them, so I'm a bit confused as to why this isn't working.

Any suggestions and help are greatly appreciated.

Was it helpful?

Solution

See JComboBox.addItemListener(ItemListener). It works reliably on mouse or keyboard selection.

OTHER TIPS

Adding mouse listeners to JComboBox is always going to be a problem. It tends to be implemented by PL&Fs as a container of other components. (Of course a PL&F may choose to do something else, breaking lots of dodgy code.) Mouse events behave very peculiarly, bubbling up to the parent contain iff there are no mouse listeners on the current component. Adding a mouse listener changes the behaviour of a component.

(I'd always put an @Override when attempting to override a method. Amazing how common and confusing it is to get it wrong. Also MouseAdapter is a bit nasty as you may use it as a MouseListener or MouseMotionListener.)

I'm not entirely sure what you are trying to do. Probably adding a listener to the combo box model would make most sense. If you want to reliably add a mouse listener, you'd probably need to go for a "glass pane" hack.

here is an example for you

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Demo Frame/SuRu");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        final JLabel jLabel = new JLabel();
        final JComboBox box = new JComboBox();
        box.addItem("");
        box.addItem("Item 1");
        box.addItem("Item 2");
        box.addItem("Item 3");
        box.addItem("Item 4");
        box.addItem("Item 5");
        box.addItem("Item 6");
        box.addItem("Item 7");
        box.addItem("Item 8");
        box.addItem("Item 9");
        box.addItem("Item 10");
        contentPane.add(new JLabel("Select Here: "));
        contentPane.add(box);
        contentPane.add(new JLabel("Seleced Item: "));
        contentPane.add(jLabel);
        box.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent arg0) {
                jLabel.setText(box.getSelectedItem().toString());
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(200, 200, 400, 100);
        frame.setVisible(true);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top