May I offer you a complete example of a table model to find out how this works. It also uses the list as data. Most importantly, you need to extend AbstractTableModel to use your own variable to store data. Here is a complete sample source code.
import java.util.ArrayList; import java.util.List; import javax.swing.table.AbstractTableModel; public class MouseClickTableModel extends AbstractTableModel { private static final long serialVersionUID = -1807522184370383957L; private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)", "Comment" }; public final Class[] mColTypes = { Integer.class, Integer.class, Integer.class, Integer.class, String.class }; private final List<MouseClick> data; public MouseClickTableModel(){ data = new ArrayList<MouseClick>(10); } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.size(); } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { final MouseClick currentRow = (MouseClick) data.get(row); switch (col) { case 0: return currentRow.getSequNb(); case 1: return currentRow.getXcoo(); case 2: return currentRow.getXycoo(); case 3: return currentRow.getDelay(); case 4: return currentRow.getComment(); } return new String(); } public Class getColumnClass(int c) { return mColTypes[c]; } public boolean isCellEditable(int row, int col) { return false; } public void updateRow(Object value, int row, int col) { } @Override public void setValueAt(Object value, int row, int col) { MouseClick currentRow = null; if (row >= data.size()) {
Then you just need to update your model with ui design.
JTable table = new JTable(new MouseClickTableModel()); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); MouseClickTableModel model = getTable().getModel()); model.insertMouseClick(0, new MouseClick(0, Integer.valueOf(xValue), Integer.valueOf(yValue), 2000, "comment"));
source share