JSlider in JTable

2019-07-07 07:59发布

For putting a JSlider into a JTable I wrote am AbstractCellEditor which implements a TableCellRendererand a TableCellEditor. It gets initialized with values from 0 to 100.

I have the strange behaviour that when I first click onta a slider it jumps to the maximum.

The second weird behaviour: I've added a ChangeListener. This Listener gets only called if I first click into the slider. A second click (which also changes the value) doesn't cause this event. Why?

public class SliderTableColumn extends AbstractCellEditor implements TableCellRenderer,
                                                                     TableCellEditor

{
    private final JSlider slRenderer;
    private final JSlider slEditor;

    private final int INITAL_VALUE;
    private final int MIN_VLAUE;
    private final int MAX_VALUE;

    public SliderTableColumn(int min, int max, int initial, ChangeListener listener)
    {
        INITAL_VALUE = initial;
        MIN_VLAUE    = min;
        MAX_VALUE    = max;

        slRenderer = new JSlider(MIN_VLAUE, MAX_VALUE);
        slEditor   = new JSlider(MIN_VLAUE, MAX_VALUE);

        slEditor.addChangeListener(listener);

        slRenderer.setUI(new CustomSliderUI(slRenderer, INITAL_VALUE));
        slEditor.setUI(new CustomSliderUI(slEditor,     INITAL_VALUE));

        slRenderer.setValue(INITAL_VALUE);
        slEditor.setValue(INITAL_VALUE);
    }

    @Override
    public Object getCellEditorValue()
    {
        return slEditor.getValue();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table,
                                                Object value,
                                                boolean isSelected,
                                                boolean hasFocus,
                                                int row,
                                                int column)
    {
        if(value != null)
        {
            slRenderer.setValue(((Integer) value).intValue());
        }
        return slRenderer;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
                                                Object value,
                                                boolean isSelected,
                                                int row,
                                                int column)
    {
        if(value != null)
        {
            slEditor.setValue(((Integer) value).intValue());
        }
        return slEditor;
    }
}

1条回答
放我归山
2楼-- · 2019-07-07 08:39

Absent your sscce, I'm speculating; but I can suggest two things to examine critically:

  1. In getTableCellEditorComponent(), the actual parameter value comes from your table model. Your setValue() invocation forwards the value to the slider's range model. An out-of-range value is pinned to the extreme.

  2. Your ChangeListener needs to fireEditingStopped(), as shown in the ItemListener of this related example.

查看更多
登录 后发表回答