I have a JTextField with a documentListener on it. I want to change the background color when I add or remove characters to this textfield. I should be using a document listener correct? It works, but it also fires when I gain and lose focus on this JTextfield, which is undesired. I do not add a focus listener on this JTextField. Here is my code, any suggestions on how I can fix my problem?
numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelNumberPlotRowsJTextField);
numPlotRowsJTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e)
{
}
@Override
public void insertUpdate(DocumentEvent e)
{
numPlotRowsJTextField.setBackground(Color.cyan);
}
@Override
public void changedUpdate(DocumentEvent e)
{
}
});
Also note that I am using JGoodies Binding which I am starting to believe is the root of this problem. Swing w/o JGoodies shouldn't be firing off document listener events by changing focus...
I figured it out. It 100% had to do with JGoodies Binding.
This code works:
ValueModel valueModelNumberPlotRowsJTextField = adapter.getBufferedModel("numberOfPlotRows");
valueModelNumberPlotRowsJTextField.addValueChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt)
{
numPlotRowsJTextField.setBackground(Color.cyan);
}
});
numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelNumberPlotRowsJTextField);
Since I am using JGoodies Binding, I have a ValueModel backing my JTextField. The listener has to be set there and not on the JTextField.