views:

786

answers:

1

First, to make my job explaining a bit easier, here's some of my code:

    JSpinner spin = new JSpinner( );
    JFormattedTextField text = getTextField( spin );

    text.addActionListener( new java.awt.event.ActionListener() {
        public void actionPerformed( java.awt.event.ActionEvent evt ) {
            // Do stuff...
        }
    } );

...

private JFormattedTextField getTextField( JSpinner spinner )
{
    JComponent editor = spinner.getEditor();

    if ( editor instanceof JSpinner.DefaultEditor )
    {
        return ( ( JSpinner.DefaultEditor )editor ).getTextField();
    }
    else
    {
        System.err.println( "Unexpected editor type: "
                           + spinner.getEditor().getClass()
                           + " isn't a descendant of DefaultEditor" );
        return null;
    }
}

So as you can see, I got that far. And indeed, when I type in a value into the text field component of the spinner (JFormattedTextField), and THEN press ENTER, it works.

What I want now is to be able to have the text field respond to ENTER without having to manually type in a new value (which sorta defeats the purpose of making a spinner out of it). How do I do that?

+3  A: 
javamonkey79
It works! Thanks.
Daddy Warbox
Happy to help. Though I'd like to know why the action listener did not work :D
javamonkey79
Beats me. :P Probably because the JSpinner was stealing away inputs somehow?
Daddy Warbox
I'd probably use getInputMap() and getActionMap(), but thanks for the post!
Jason S