import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableModel; public class BuildGraph { private static void buildGUI(){ JFrame.setDefaultLookAndFeelDecorated(true); //Build our JFrame JFrame frame = new JFrame("Table Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Put the Column Names into a String Array String[] columnNames = {"One", "Two", "Three", "Four"}; //Put the data into a nested Object Array. Object[][] data = { {"This", "Is", "The", new Boolean(true)}, {"This", "Is", "The", new Boolean(false)}, {"This", "Is", "The", new Boolean(true)} }; //Build the JTable passing it both arrays //JTable table = new JTable (data, columnNames); // TableModel model = new this.MyTableModel(); //Build a JTable using the custom table Model JTable table = new JTable(new BuildGraph().MyTableModel()); //Add the table to a ScrollPane, this intermediate step //is necessary in order for the column names to be displayed JScrollPane scrollPane = new JScrollPane(table); //Add the Scrollpane, and do all the other //Gui Work to get it ready to present, then make //it visible. frame.add(scrollPane); frame.setSize(400, 400); frame.pack(); frame.setVisible(true); } //This extension of the AbstractTableModel is reconstituted //from the demo given in Suns Java tutorial, the original //file is located at : //http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableDemo.java class MyTableModel extends AbstractTableModel{ //We need to declare these in order to //have functions that work with them. private String[] columnNames; private Object[][] data; //Setter method for column Names public void setColumnNames(String[] a){ columnNames = a; } //Setter method for column Data public void setDate(Object[][] a){ data = a; } //These are functions required in order //to extend the AbstractTableModel. public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } /* * JTable uses this method to determine the default renderer/ * editor for each cell. If we didn't implement this method, * then the last column would contain text ("true"/"false"), * rather than a check box. */ public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /* * Don't need to implement this method unless your table's * editable. */ public boolean isCellEditable(int row, int col) { //Note that the data/cell address is constant, //no matter where the cell appears onscreen. if (col < 2) { return false; } else { return true; } } } public static void main(String[] args){ javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run(){ buildGUI(); } }); } }