Best method to fill in tablemodels getColumnClass
Hello,
in my application i have several JTables. The data represented on it is derived from a database. Some columns are booleans, but because my database doesn't support booleans I use VARCHAR(1) which is a String in Java.
All my tables are using a tablemodel which extends the DefaultTableModel. I want the booleans to be represented as a checkbox in my JTable.
1) Where is the best place to convert the string values to boolean values (I suppose somewhere in the tablemodel) ?
2) To get the classes of my columns I can derive them from the database itself. But should i get them every time the getColumnClass() is called or should I load them when I create my tablemodel?
What would be the best approach here?
I ask these questions because I know that you must be careful when changing the tablemodel to avoid that you tables are becoming slow.
[891 byte] By [
myncaa] at [2007-11-27 2:36:59]

# 1
1) Convert the data when you add it to the TableModel so its only done once.
2) The Object added to the TableModel is of the correct class. You could create an array that stores the class of each column when you create the model, but from a speed point of view this isn't as important as efficient rendering code. If all your columns will have non-null data then you could just use generic code like this:
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;
}
> I know that you must be careful when changing the tablemodel to avoid that you tables are becoming slow
Then main concern here is for rendering of each cell. The data is only loaded into the model once so that why you would do all the conversion up front. However renderering of each cell is done much more frequently, every time you change the selection of a row, two rows get repainted. Every time you scroll multiple rows get painted. So the bigger concern is the the renderer is very efficient and doesn do an unnecessary processing.