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
Roedy Green - 21 Jun 2008 21:09 GMT
On Thu, 19 Jun 2008 08:23:34 -0700 (PDT), Ralph Boland
<rpboland@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>I want to know when the text field of a JSpinner is modified so that I
>can turn on a radio button.
See the basic code for monitoring the JSpinner value change at
http://mindprod.com/jgloss/jspinner.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Ralph Boland - 25 Jun 2008 01:03 GMT
On Jun 21, 2:09 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Thu, 19 Jun 2008 08:23:34 -0700 (PDT), Ralph Boland
> <rpbol...@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 8 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
The code you pointed me to doesn't do what I need. I want to know
when the user has modified the text in the
TextField component of a JSpinner directly. Better still, I want to
know when the mouse has been clicked in
the TextField, that is even before the user has modified any actual
text.
Ralph Boland
Roedy Green - 02 Jul 2008 03:21 GMT
On Thu, 19 Jun 2008 08:23:34 -0700 (PDT), Ralph Boland
<rpboland@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>I want to know when the text field of a JSpinner is modified so that I
>can turn on a radio button.
A JSpinner as actually a little constellation of components.
see http://mindprod.com/jgloss/jspinner.html#TIPS

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roland de Ruiter - 05 Jul 2008 14:59 GMT
> I want to know when the text field of a JSpinner is modified so that I
> can turn on a radio button.
[quoted text clipped - 57 lines]
>
> 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) {
...
}
});

Signature
Regards,
Roland
Ralph Boland - 09 Jul 2008 16:03 GMT
On Jul 5, 7:59 am, Roland de Ruiter <roland.de.rui...@example.invalid>
wrote:
> On 19-6-2008 17:23, RalphBolandwrote:
>
[quoted text clipped - 84 lines]
>
> Roland
Two brownie points to you.
I did exactly as you said
and it worked exactly as you said.
Thanks
Ralph Boland