关于jtable细胞编辑的问题 [英] Question on jtable cell editors in swing

查看:132
本文介绍了关于jtable细胞编辑的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个用作选项列表的组件,用户可以选择是否选择。

从视觉上我认为最好在UI中显示(如果有的话)一个更好的方法,请告诉我)如下:

I want to implement a component that serves as a list of options, that a user can choose to select or not.
Visually I thought that it would be best presented in a UI (if there is a better approach please tell me) as follows:

无论如何,我认为这可以通过 JTable (单列)并使用来实现JCheckBox 作为单元格编辑器。

我试了但是没有用。

代码示例:

Anyway, I thought that this could be implemented via a JTable (single column) and using a JCheckBox as a cell editor.
I tried it but did not work.
Example of code:

public class ListRenderer extends JFrame {

    JCheckBox checkbox = new JCheckBox("Test");
    DefaultCellEditor dce1 = new DefaultCellEditor(checkbox);

    public ListRenderer(){          

        Object[][] data =  {   {"Test"} };
        String[] columnNames = {"Options"};

        DefaultTableModel model = new DefaultTableModel(data,columnNames);

        JTable table = new JTable(model){

            public TableCellEditor getCellEditor(int row, int column)            
            {               
                return dce1;                
            }

        };
        JScrollPane scrollPane = new JScrollPane( table );        
        getContentPane().add( scrollPane );
    }

当帧出现时我会看到测试表但它看起来不像一个复选框(如示例图片中所示)。

如果我单击单元格,它会变成一个复选框(左侧单击按钮而不是右侧)但文本更改显示 true false !它不会继续显示测试

更多文本取决于我是否继续按下单元格。

如果我更改 JCheckBox JComboBox 据我所知,行为是正确的。

任何人都可以告诉我,我做错了什么在这里?

谢谢!

What happens is that when the frame appears I see the "Test" in the table but it does not appear like a checkbox (as in the example image).
If I click on the cell, it turns into a checkbox (click button on the left and not right) but the text changes to show either true or false! It does not keep showing "Test"
More over the text depends on whether I keep pressing on the cell or not.
If I change the JCheckBox to a JComboBox the behavior is correct as far as I can tell.
Can anyone please tell me what am I doing wrong here?
Thanks!

推荐答案

要呈现为 JCheckBox 默认,表格模型必须返回 Boolean.class 作为该列的类型。如果您使用的是 DefaultTableModel ,您必须相应地覆盖 getColumnClass()。这是一个相关的示例

To be rendered as a JCheckBox by default, the table's model must return Boolean.class as the type for that column. If you are using DefaultTableModel, you will have to override getColumnClass() accordingly. Here's a related example.

附录:请在示例中注明编辑器的私有实例ValueRenderer 可以直接应用 ItemEvent ,而不是通过 setValueAt()

Addendum: Note in the example that the editor's private instance of ValueRenderer can apply ItemEvent directly, instead of via setValueAt().

附录:示例已更新,以反映正确的模型 - 视图工作流程。

Addendum: The example has been updated to reflect the correct model-view workflow.


无论如何都会调用setValueAt() 。通过调试验证

setValueAt() is called anyway. Verified it via debugging

如果您进入 setValueAt() ,您会看到此空实现已提供,因此如果用户的数据模型不可编辑,则用户无需实施此方法。

If you step into setValueAt(), you'll see that "This empty implementation is provided so users don't have to implement this method if their data model is not editable."

这篇关于关于jtable细胞编辑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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