可以实例化DocumentListener类型 [英] Can instantiate the type DocumentListener
问题描述
我需要这样做: JTextField的价值变动监听器
我正在尝试使用Condemwnci的解决方案,但我收到了错误:
textField.getDocument()。addDocumentListener(new DocumentListener()
在我的情况下是:
jtxtfBuscarInv.getDocument()。addDocumentListener(new DocumentListener()
I'm trying the solution of Condemwnci, but I'm getting that error in the line:
textField.getDocument().addDocumentListener(new DocumentListener()
that in my case is:
jtxtfBuscarInv.getDocument().addDocumentListener(new DocumentListener()
在我的情况下,我想在Jtable中更新行,所以我的方法是这样的:
ordenador.setRowFilter(RowFilter.regexFilter( jtxtfBuscarInv.getText(),0));
In my case I want update rows in a Jtable, so my method will be this:
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
我在linux,ubuntu 11.10上,如果重要的话,使用Eclipse。
I'm on linux, ubuntu 11.10, using Eclipse if that matters.
编辑:
我不明白为什么,但它有效在下一个方式:
I don't understand why, but it works in next way:
textField.getDocument().addDocumentListener(this);
然后重写implements方法,而不是全部在同一行中。
and then overrride the implements methods, instead of doing all in the same lines.
推荐答案
添加 DocumentListener
的两种方法应该是完全相同的。以下示例为每个事件列出 all 类型为 DocumentListener.class
的侦听器。显而易见的是 DocumentListeners
本身,而 匿名内部类 具有(依赖于实现)名称 DocumentListeners $ 1
。两者都实现了 DocumentListener
接口。其他是典型文本组件维护的一部分。请注意,会显示两个副本,每个副本都会添加一个。
The two approaches to adding a DocumentListener
should be effectively identical. The example below lists all the listeners of type DocumentListener.class
for each event. The obvious one is DocumentListeners
itself, while the anonymous inner class has the (implementation dependent) name DocumentListeners$1
. Both implement the DocumentListener
interface. The others are part of typical text component maintenance. Note that two copies are shown, one from each listener added.
控制台:
javax.swing.text.JTextComponent$InputMethodRequestsHandler@5090d8ea
DocumentListeners$1@559113f8
DocumentListeners[,0,0,128x38,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
javax.swing.plaf.basic.BasicTextUI$UpdateHandler@27b62aab
javax.swing.text.DefaultCaret$Handler@28ab54eb
javax.swing.text.JTextComponent$InputMethodRequestsHandler@5090d8ea
DocumentListeners$1@559113f8
DocumentListeners[,0,0,128x38,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
javax.swing.plaf.basic.BasicTextUI$UpdateHandler@27b62aab
javax.swing.text.DefaultCaret$Handler@28ab54eb
代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
/** @see http://stackoverflow.com/questions/8283067 */
public class DocumentListeners extends JPanel implements DocumentListener {
JTextField jtf = new JTextField("StackOverflow!");
public DocumentListeners() {
this.add(jtf);
jtf.getDocument().addDocumentListener(this);
jtf.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
print(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
print(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
print(e);
}
});
}
private void print(DocumentEvent e) {
AbstractDocument ad = (AbstractDocument) jtf.getDocument();
for (DocumentListener dl : ad.getListeners(DocumentListener.class)) {
System.out.println(dl);
}
}
@Override
public void insertUpdate(DocumentEvent e) {
print(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
print(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
print(e);
}
private void display() {
JFrame f = new JFrame("DocumentListeners");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DocumentListeners().display();
}
});
}
}
这篇关于可以实例化DocumentListener类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!