Is there a convenient way to use a spinner as an editor in a Swing JTable?

后端 未结 4 1865
梦如初夏
梦如初夏 2021-01-06 06:53

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 looke

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 07:36

    ... 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();
        }
    }
    

提交回复
热议问题