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.
See
JComboBox.addItemListener(ItemListener). It works reliably on mouse or keyboard selection.