JCombobox focusLost doesn't fire - why?

I have JCombobox code in my code. I added a FocusLost event . But he still didn’t shoot. I tried a lot of time but could not find a 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 the console. Please suggest me what I am doing wrong.

+7
source share
2 answers
  • FocusListener is not suitable for listening to JComboBox , in general with another Listener (s) it can create an infinite loop (especially Editable JComboBox)

  • FocusListener is asynchronous, sometimes it’s too difficult to catch events, these are the correct orders, especially in cases where JComponents added another Listener (s) too

example of how to listen to Focus from a JTextField / JFormattedTextField

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxTwo extends JFrame implements ItemListener { private static final long serialVersionUID = 1L; private JComboBox mainComboBox; private JComboBox subComboBox; public ComboBoxTwo() { String[] items = {"Select Item", "Color", "Shape", "Fruit"}; String[] subItems1 = {"Select Color", "Red", "Blue", "Green"}; mainComboBox = new JComboBox(items); mainComboBox.addItemListener(this); mainComboBox.addFocusListener(fcsListener); add(mainComboBox, BorderLayout.WEST); subComboBox = new JComboBox(subItems1); subComboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXX"); subComboBox.addItemListener(this); add(subComboBox, BorderLayout.EAST); } @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { if (e.getSource() == mainComboBox) { System.out.println("Source : mainComboBox"); } else if (e.getSource() == subComboBox) { System.out.println("Source : subComboBox"); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new ComboBoxTwo(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } // private FocusListener fcsListener = new FocusListener() { @Override public void focusGained(FocusEvent e) { dumpInfo(e); } @Override public void focusLost(FocusEvent e) { dumpInfo(e); } private void dumpInfo(FocusEvent e) { System.out.println("Source : " + name(e.getComponent())); System.out.println("Opposite : " + name(e.getOppositeComponent())); System.out.println("Temporary: " + e.isTemporary()); final Component c = e.getComponent();//works for editable JComboBox too if (c instanceof JFormattedTextField) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ((JFormattedTextField) c).selectAll(); } }); } else if (c instanceof JTextField) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ((JTextField) c).selectAll(); } }); } } private String name(Component c) { return (c == null) ? null : c.getName(); } }; } 
+6
source

I found a very simple way to solve this problem.

The default JComboBox editor has an internal class BasicComboBoxEditor $ BorderlessTextField, which is a component that gains and loses focus.

You can access it simply

 Component component = comboBox.getEditor().getEditorComponent(); if (component instanceof JTextField) JTextField borderlesstextfield = (JTextField) borderless; 

Then add a focus listener like any JTextField

 borderlesstextfield.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { } }}); 

Now you have a FocusListener that will respond as expected to win and lose focus for the ComboBox

+5
source

All Articles