import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class MyTableModel extends JFrame { public MyTableModel() { super("Abstract Model JTable Test"); TableModel tm = new AbstractTableModel() { //This anonymous inner class is our new table model. //The data is contained in a two-dimensional String array //and can not be modified. No events are fired. String[][] data = { {"This", "is"}, {"a", "Test"} }; String[] headers = {"Column", "Header"}; public int getRowCount() { return data.length; } public int getColumnCount() { return headers.length; } public Object getValueAt(int r, int c) { return data[r][c]; } //This method is implemented in AbstractTableModel to //name the columns A, B, C... So we do not have to //write our own implementation if we are staisfied with //those names. public String getColumnName(int c) { return headers[c]; } }; //Create a table that use our own table model. JTable jt = new JTable(tm); //Place the table in a scroll pane and display it. JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); setSize(300, 200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } public static void main(String args[]) { new MyTableModel(); } }