I have a JCombobox
in my code. I have added the FocusLost event
. But it didn't fired anyway. I have tried lots of time but didn't find solution.
jcbItemType.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jcbItemTypeFocusLost(evt);
}
});
private void jcbItemTypeFocusLost(java.awt.event.FocusEvent evt)
{
// TODO add your handling code here:
System.out.println("name=" + ((Component) evt.getSource()).getName());
System.out.println("index=" + jcbItemType.getSelectedIndex());
}
But nothing is printed in console. Please suggest me what I am doing wrong.
FocusListener isn't proper Listener for JComboBox, altogether with another Listener(s) can creating endless loop (especially Editable JComboBox),
FocusListener is asynchronous, sometimes is too hard to catch events is right orders especially in the cases that JComponents has added another Listener(s) too
example how to listening for Focus from derived
JTextField / JFormattedTextField
I have found a very simple way to solve this.
The JComboBox default editor has an internal class BasicComboBoxEditor$BorderlessTextField that is the component that gets and loses focus.
It can be accessed simply by
Then add a focus listener like you would to any JTextField
Now you have a FocusListener that will respond as expected to gain and loss of focus for the ComboBox