Java,使用rs2xml填充jTable时,将布尔列更改为jTable中的复选框 [英] Java, changing boolean column to checkbox in jTable when using rs2xml for populating jTable

查看:51
本文介绍了Java,使用rs2xml填充jTable时,将布尔列更改为jTable中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通过rs2xml填充mysql db数据的jTable

I am using a jTable which is populated with mysql db data using rs2xml

table.setModel(DbUtils.resultSetToTableModel(rs));

我有一些由布尔值显示的列,但这些列必须成为复选框.我了解我必须编写自己的AbstractTableModel,但我不知道如何...

I have some columns that are displayed by boolean values, but these must become checkboxes. I understand that i have to write my own AbstractTableModel, but I don't know how...

你们中的一个可以举一个例子,说明如何扩展AbstractTableModel并在代码中使用它吗?

Can one of you give an example of how you extend the AbstractTableModel and use it in your code?

推荐答案

我有一些按布尔值显示的列,但这些列必须成为复选框.

I have some columns that are displayed by boolean values, but these must become checkboxes.

然后,您可以覆盖JTable的 getColumnClass(...)方法:

Then you can override the getColumnClass(...) method of the JTable:

JTable table = new JTable(...)
{
    @Override
    public Class getColumnClass(int column)
    {
        for (int row = 0; row < getRowCount(); row++)
        {
            Object o = getValueAt(row, column);

            if (o != null)
            {
                return o.getClass();
            }
        }

        return Object.class;
    }
};

或者根据建议,您可以创建自己的TableModel.这并不困难,再次,您真正需要做的就是实现 getColumnClass(...)方法,但是您需要编写自己的代码以将数据加载到TableModel中.

Or as suggested you can create your own TableModel. This is not difficult, again all you really need to do is implement the getColumnClass(...) method, but you need to write your own code to load the data into the TableModel.

请参见 查看全文

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