I need to do this: Value Change Listener to JTextField
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()
In my case I want update rows in a Jtable, so my method will be this:
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
I'm on linux, ubuntu 11.10, using Eclipse if that matters.
Edit:
I don't understand why, but it works in next way:
textField.getDocument().addDocumentListener(this);
and then overrride the implements methods, instead of doing all in the same lines.
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.
Console:
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
Code:
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();
}
});
}
}