有没有一种方便的方法可以在Swing JTable中使用微调器作为编辑器? [英] Is there a convenient way to use a spinner as an editor in a Swing JTable?

查看:221
本文介绍了有没有一种方便的方法可以在Swing JTable中使用微调器作为编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理的数值数据经常被上调或下调0.01 * Value_of_variable,因此与通常的文本单元格相比,微调器看起来是个不错的选择。

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.

我看过DefaultCellEditor,但它只会带文本字段,组合框或复选框。

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?

推荐答案

...并覆盖getCellEditorValue()方法:

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

这篇关于有没有一种方便的方法可以在Swing JTable中使用微调器作为编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆