Java, changing boolean column to checkbox in jTable when using rs2xml for populating jTable

时光总嘲笑我的痴心妄想 提交于 2021-02-11 13:36:51

问题


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

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

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...

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


回答1:


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

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;
    }
};

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.

See the TableFromDatabase.java example code found Table From Database for example code to replace your DbUtils class.



来源:https://stackoverflow.com/questions/32734394/java-changing-boolean-column-to-checkbox-in-jtable-when-using-rs2xml-for-popula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!