Search code examples
javaswingjtableabstracttablemodeldefaulttablemodel

Add column to exiting TableModel


I have a class;

public class A extends AbstractTableModel
{
 ...
}

Using ResultSetMetaData I build the TableModel to match my result set from the database.

public class B extends JPanel
{
 ...
}

In class B where I extends JPanel and added class A to show my table. I want to be able to add new columns base on a condition to the table model. I have tried googling around but most example shown are based on the DefaultTableModel not AbstractTableModel.

Do anyone know how to achieve this?


Solution

  • Just extend DefaultTableModel and then you have access to all of its methods. DefaultTableModel also extends AbstractTableModel, so references to AbstractTableModel in other parts of your code aren't going to break.