String columnNames = new String[] {"ID", "Name ", "Address 1 ", "Address 2 ", "Country"}; for (int i = 0; i < columnNames.length; i++) { TableColumn column = table.getColumnModel().getColumn(table.convertColumnIndexToView(i)); column.setHeaderValue(columnNames[i]); } table.getTableHeader().repaint();
public boolean isCellEditable
// updateTable.java import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.*; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */ class updateTable extends AbstractTableModel { private Object[] columnNames; private Object[][] data; Debug error = new Debug(); public updateTable(Object[][] dataIn, Object[] colsIn){ columnNames = colsIn; data = dataIn; } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } 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; // we don't want any fields editable, but you could have some if you wanted. //} else { // return true; //} } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { error.add("row: " + row + " - Col: " + col + " - Value: " + value); data[row][col] = value; fireTableCellUpdated(row, col); } }
String columnNames = new String[] {"ID", "Name ", "Address 1 ", "Address 2 ", "Country"}; Object[][] data = { {"", "", "", "", ""} // empty }; JTable table = new JTable(data, columnNames); table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); // can only select 1 row at a time table.setModel(new updateTable(data, columnNames));