TableModel tableModel = new DefaultTableModel(data, columnNames); JTable table = new JTable(tableModel); table.setAutoCreateColumnsFromModel(true);
public class MyTableModel extends AbstractTableModel { //code to set data and column names public MyTableModel(ListIn this example, a custom table model is created by extending the AbstractTableModel class. The constructor takes a list of data and column names, which are used to set the model. The updateData method allows for the data in the model to be updated. The JTable is then created using the custom model and setAutoCreateColumnsFromModel is set to true. Package library: javax.swing.data, String[] columnNames) { //code to set data and column names } public void updateData(List newData) { //code to update data fireTableDataChanged(); } } MyTableModel model = new MyTableModel(data, columnNames); JTable table = new JTable(model); table.setAutoCreateColumnsFromModel(true);