views:

92

answers:

2

I deal with numeric data that is often edited up or down by 0.01*Value_of_variable, so a spinner looks like a good choice compared to a usual text cell.

I've looked at DefaultCellEditor but it will only take text fields, combo boxes or check boxes.

Is there a convenient way to use a spinner?

+2  A: 

Simply extend DefaultCellEditor and overwrite the getTableCellEditorComponent() method to return a JSpinner.

Michael Borgwardt
+1 @Uri - Also, see the "Using Other Editors" http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender
ssakl
+2  A: 

... and overwrite the getCellEditorValue() method:

class SpinnerEditor extends DefaultCellEditor
{
    private JSpinner spinner;

    public SpinnerEditor()
    {
     super( new JTextField() );
     spinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 5));
     spinner.setBorder( null );
    }

    public Component getTableCellEditorComponent(
     JTable table, Object value, boolean isSelected, int row, int column)
    {
     spinner.setValue( value );
     return spinner;
    }

    public Object getCellEditorValue()
    {
     return spinner.getValue();
    }
}
camickr