Re: can't listen to TextA
To: comp.lang.java.gui
On 19-6-2008 17:23, Ralph Boland wrote:
I want to know when the text field of a JSpinner is modified so that I
can turn on a radio button.
So I created the following class:
class ButtonSettingSpinnerNumberModel extends SpinnerNumberModel
implements DocumentListener {
AbstractButton button;
ButtonSettingSpinnerNumberModel(JSpinner spinner, AbstractButton
jButton,
int value, int minimum, int maximum, int stepSize) {
super(value, minimum, maximum, stepSize);
button = jButton;
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor)
spinner.getEditor();
editor.getTextField().getDocument().addDocumentListener(this);
spinner.setModel(this);
}
public void changedUpdate(DocumentEvent e) {
button.setSelected(true);
}
public void removeUpdate(DocumentEvent e) {
button.setSelected(true);
}
public void insertUpdate(DocumentEvent e) {
button.setSelected(true);
}
}
Now I create an instance of the this class passing as parameters my
JSpinner and JRadioButton.
Now when the text field of the spinner is modified one of the three
update methods
(required for the DocumentListener Interface)
should be called because my instance is listening to the text field of
the spinner.
However my button is never selected and with the debugger I determined
that
the neither update method is invoked.
I do the same thing with A JTextField object and a different
JRadioButton and everything
works fine.
Any suggestions as to why this doesn't work?
Final note: I would like to be able to update my JRadioButtons when
the user clicks the
cursor into the JTextField (my proper JTextField or the one in the
JSpinner) rather than wait for
the text to be modified.
How can this be done?
Thanks to anyone who can help and I hope the problem/solutions are of
interest to others.
Ralph Boland
Could be an order problem. The call to setModel in the
ButtonSettingSpinnerNumberModel constructor -in this case- has a side
effect: it also creates a new spinner editor (plus textfield and document).
Because you call it after hooking up the DocumentListener, this listener
still listens to the "old" Document, which of course won't change any more.
Probably it can be solved by moving line containing the setModel call to
just after the line calling super() constructor.
For your second question "when the user clicks the cursor into the
JTextField", you could use a FocusListener:
editor.getTextField().addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
...
}
public void focusGained(FocusEvent e) {
...
}
});
--
Regards,
Roland
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24