im trying to code a DocumentListener in order to catch the change into a textField.
I have a panel which include another panel in which i put a textField, i have tried a lot of formulas but it didnt work
This is the panel
public JPanelTASAS() {
initComponents();
txtTASA.getDocument().addDocumentListener(new BecomingYellowDocumentListener(txtTASA));
}
private static class BecomingYellowDocumentListener implements DocumentListener {
private utilesGUIx.JTextFieldCZ textField;
public BecomingYellowDocumentListener(utilesGUIx.JTextFieldCZ textField) {
this.textField = textField;
}
@Override
public void insertUpdate(DocumentEvent e) {
textField.setBackground(Color.yellow);
System.out.println("Prueba");
}
@Override
public void removeUpdate(DocumentEvent e) {
textField.setBackground(Color.yellow);
System.out.println("Prueba");
}
@Override
public void changedUpdate(DocumentEvent e) {
textField.setBackground(Color.yellow);
System.out.println("Prueba");
}
}
The next one is the principal Panel where the other panel is included
public JPanelTRANSMISIONES() {
initComponents();
anadirPaneles();
}
With this code inside initComponents
jPanelTASAS1 = new gestionTrafico.forms.JPanelTASAS();
And for the record the code of utilesGUIx.JTextFieldCZ
public JTextFieldCZ() {
super();
enableEvents(AWTEvent.FOCUS_EVENT_MASK);
enableEvents(AWTEvent.KEY_EVENT_MASK);
setDocument(createDefaultModel());
}
public void setDocument(Document doc) {
if (doc != null) {
doc.putProperty("filterNewlines", Boolean.TRUE);
}
super.setDocument(doc);
}
Just to be clear, if if apply this listener to a JTextField of the Principal Panel it works, I think that the problem is adding a document listener to a panel inside another panel. Is it possible ?
Thank you very much in advance for your help
EDIT: I realice that if i change the value of the textfield hardcoding, the documentlistener works. But if i change the value of the textfield in the panel it doesnt.
Guessing: your problem is simply that just changing the background doesn't automatically trigger a repaint of the affected UI element.
In other words: you will probably see those messages on stdout; but in order for your UI to change, you should call
repaint()on your frame or panel.See here for some common solutions to common painting problems.
But given your latest comment, you are not even there yet. I guess you have to look into the details of using a DocumentListener more carefully, for example by studying this here.