Search code examples
javaswingfocustablecelleditorjspinner

Use JSpinner like JTable cell editor


i'm using a JSpinner like a table cell editor, i have one annoying problem:

The cell remains in NON-editable mode until i click into it, for NON-editable i mean that i can't write into it(it has not focus so it doesn't accept inputs keyboard) but i can change the value with up-down arrows(of keyboard).

So, what i have to do to focus my table cell as soon as i press a key when it is selected?

Except for that problem my SpinnerEditor class works quite well.

Thanks all.


Solution

  • Ok, my solution now is this:

    spinner.addFocusListener(new FocusListener() {
    
        public void focusGained(FocusEvent e) {
    
            // editor is ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
            // necessary to set focus on the text component of jspinner
            editor.requestFocus();
    
            // this if you want to select the displayed text of jspinner
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("run");
                    editor.selectAll();
                }
            });
        }
    
        public void focusLost(FocusEvent e) {}
    });