如何知道可编辑JComboBox的文本何时更改? [英] How can I know when the text of an editable JComboBox has been changed?

查看:537
本文介绍了如何知道可编辑JComboBox的文本何时更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编辑的 JComboBox,我希望在文本更改时通过键入或选择来执行某些操作。在这种情况下,文本是一个模式,我想验证模式是否有效并显示导致某些测试数据的匹配。

I have an editable JComboBox where I want to take some action whenever the text is changed, either by typing or selection. In this case, the text is a pattern and I want to verify that the pattern is valid and show the matches that result in some test data.

完成了显而易见的事情,附上一个ActionHandler,我发现,对于打字,这个事件似乎不可靠,最好(选择很好)。当 因键入而触发时,检索到的文本(使用getEditor()。getItem(),因为getSelectedItem()只获取从列表中选择的文本)似乎是当最后一个事件被触发时的文本 - 也就是说,它始终缺少在触发动作事件之前立即输入的字符。

Having done the obvious, attach an ActionHandler, I have found that, for typing, the event seems to fire unreliably, at best (selection is fine). And when it does fire as a result of typing, the text retrieved (using getEditor().getItem(), since getSelectedItem() only gets the text when it was selected from the list) seems to be the text as it was when the last event was fired - that is, it's always missing the character was typed immediately before the action event was fired.

我期待这个动作在一段短暂的延迟(500毫秒到1秒)之后开火的事件,但它似乎会在键控时立即触发(如果它被解雇)。

I was expecting the action event to fire after some short delay (500ms to 1 second), but it seems immediately fired upon keying (if it is fired at all).

唯一可行的替代方案我可以想到的是,只需在焦点上获得一个1秒的计时器,在焦点丢失时将其杀死,并在内容与上次不同的情况下作为计时器操作进行工作。

The only workable alternative I can think of is to simply start a 1 second timer on focus-gained, killing it on focus-lost and doing the work as the timer action if the content is different from last time.

有任何想法或建议吗?

代码片段不是特别有趣:

The code snippets are not particularly interesting:

find.addActionListener(this);
...
public void actionPerformed(ActionEvent evt) {
    System.out.println("Find: "+find.getEditor().getItem());
    }


推荐答案

动作监听器通常只是当你按下回车键时触发,或者将焦点移离组合框的编辑器。截取编辑器的单个更改的正确方法是注册文档侦听器:

The action listener is typically only fired when you hit enter, or move focus away from the editor of the combobox. The correct way to intercept individual changes to the editor is to register a document listener:

final JTextComponent tc = (JTextComponent) combo.getEditor().getEditorComponent();
tc.getDocument().addDocumentListener(this);

DocumentListener接口具有在修改支持编辑器的文档时调用的方法(insertUpdate,removeUpdate,changeUpdate)。

The DocumentListener interface has methods that are called whenever the Document backing the editor is modified (insertUpdate, removeUpdate, changeUpdate).

您还可以使用匿名类来更精细地控制事件的来源:

You can also use an anonymous class for finer-grained control of where events are coming from:

final JTextComponent tcA = (JTextComponent) comboA.getEditor().getEditorComponent();
tcA.getDocument().addDocumentListener(new DocumentListener() { 
  ... code that uses comboA ...
});

final JTextComponent tcB = (JTextComponent) comboB.getEditor().getEditorComponent();
tcB.getDocument().addDocumentListener(new DocumentListener() { 
  ... code that uses comboB ...
});

这篇关于如何知道可编辑JComboBox的文本何时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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