问题
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