CheckBox in JTable is not showing when data is fed to the Table from database but shows true false values

StackOverflow https://stackoverflow.com/questions/22569432

  •  19-06-2023
  •  | 
  •  

Question

I have a JTable which is fed data using DefaultTableModel. Now my question is when there was in the table and there were only empty rows in the table, the columns with Boolean type were showing in check box form. But when I added data to the table from the database, check box were gone and simple true and false sere shown by the table. I have checked for the solution and I found one on the link (in the comments of the question). It says I need to override getColumnClass method but I don't know I should implement this method of JTable class or DefaultTableModel class? The code is too simple to post here, it only fetches the data from database in form of LinkedList and then fed to the DefaultTableModel constructor by converting it into Vector class Type along with column names as per required by the constructor. and then object of the model is passed to JTable.setModel() method.

Was it helpful?

Solution

By overriding this method of the model:

@Override
public Class<?> getColumnClass(int i) {
    return super.getValueAt(0, i).getClass(); 
}

Of course, you can add other code according to the requirement in this overridden function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top