I am learning on how to use JTables in Swing. I have already figured out to connect to a database and retrieve the resultset. I am also able to display the data by using the first option (see below).
a) I want to make sure I am understanding the idea of using/passing a TableModel versus directly passing the rows and columns is to have the ability to use built in methods that are available in AbstractTableModel; DefaultTableModel and ListTableModel classes.
b) What is a custom TableModel?
So far I have come across 4 ways:
- Pass columns and rows as objects directly to JTable constructor.
ex:
JTable tab = new JTable(Object [][] rows, Object[] cols);
2. Create a table model from a class that implements AbstractTableModel.
ex:
MyModel model = new MyModel(Object[][] obj1, String[] header);
//MyModel is a class that extends AbstractTableModel.
//MyModel has an ArrayList<Object[]> to store obj1[]
//MyModel implements getRowCount(), getColumnCount() and
getValueAt(int rowIndex, int columnIndex) and also getColumnName(int index)
JTable tab = new JTable(model);
3.Create a table model from a class that implements DefaultTableModel.
ex:
DefaultTableModel model = new DefaultTableModel(String data[][],String col[]);
(or)
DefaultTableModel model = DefaultTableModel(Vector data, Vector columnNames)
JTable table = new JTable(model);
4.ListTableModel