无法触发可编辑JCombobox上的FocusListener [英] FocusListener on editable JCombobox not firing

查看:129
本文介绍了无法触发可编辑JCombobox上的FocusListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先: 抱歉,我无法提供SSCCE.我试图在一个小项目上重现此问题,但没有成功,或者我应该说成功,因为它在那里工作!

这是我的问题: 我有一个可编辑的JCombobox,它应通过单击编辑器组件来监听焦点事件.但这不是.

这是我附加侦听器的代码段:

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");
    }
  });

我已经尝试删除组合框上的所有其他侦听器,删除观察者模式,保护组合框并将侦听器附加到每个孩子上,...

我也尝试过像这篇文章中所写的那样重新实现它: JCombobox focusLost没有触发,为什么?

是否有调试事件的好方法?

可以在Github上查看整个项目: https://github.com/nexxx/Database-Analyzer

可以在此处找到工具栏类(从第98行开始的代码): https://github. com/nexxx/Database-Analyzer/blob/master/src/dba/gui/auxClasses/toolBars/ToolBar.java

解决方案

相同的代码在某些情况下会导致问题,而在其他情况下不会导致问题,这使我怀疑您的代码可能无法在

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

解决方案

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.

这篇关于无法触发可编辑JCombobox上的FocusListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆