Search code examples
javaswingjcomboboxfocuslistener

FocusListener on editable JCombobox not firing


First of all: Sorry that I cannot provide a SSCCE. I tried to recreate this problem on a small project, but without success, or should I say with success, because its working there!

So here is my Problem: I have an editable JCombobox, which should listen to a focus event by clicking in the editor component. But it doesn´t.

Here is a code snippet where I attach the listener:

cmbZoom.setToolTipText(locale.getString("GUI_ZoomFactor"));
cmbZoom.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
cmbZoom.setPreferredSize(new Dimension(88, 29));
cmbZoom.setEditable(true);
((JTextField)cmbZoom.getEditor().getEditorComponent()).setHorizontalAlignment(JTextField.CENTER);
cmbZoom.getEditor().getEditorComponent().addFocusListener(
  new FocusListener(){
    public void focusGained(FocusEvent arg0) {
      System.out.println("GAINED");
    }

    public void focusLost(FocusEvent arg0) {
      System.out.println("LOST");
    }
  });

I have already tried to remove all other listener on the combobox, remove the observer pattern, make the combobox protected and attach the listener on each child,...

I have also tried to reimplement it like written on this post: JCombobox focusLost is not firing-why is that?

Is there a good way to debug events?

The whole project can be viewed on Github: https://github.com/nexxx/Database-Analyzer

The toolbar class can be found here (code starting on line 98): https://github.com/nexxx/Database-Analyzer/blob/master/src/dba/gui/auxClasses/toolBars/ToolBar.java


Solution

  • The fact that the same code causes problems in some circumstances but not others leads me to suspect your code may not be executing on the AWT event dispatch thread.