import com.sun.java.swing.JTable; import com.sun.java.swing.table.AbstractTableModel; import com.sun.java.swing.DefaultCellEditor; import com.sun.java.swing.table.TableColumn; import com.sun.java.swing.JButton; import com.sun.java.swing.JCheckBox; import com.sun.java.swing.JScrollPane; import com.sun.java.swing.JPanel; import com.sun.java.swing.JFrame; import com.sun.java.swing.SwingUtilities; import com.sun.java.swing.JOptionPane; import java.awt.*; import java.awt.event.*; /** * This is exactly like TableEditDemo, except that it uses a * custom cell editor to bring up a dialog for surname data. */ public class TableDialogEditDemo extends JFrame { private boolean DEBUG = false; public TableDialogEditDemo() { super("TableDialogEditDemo"); MyTableModel myModel = new MyTableModel(); JTable table = new JTable(myModel); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Set up real input validation for the integer column. setUpIntegerEditor(table); //Set up dialog as editor for the Sport column. TableColumn surnameColumn = table.getColumnModel().getColumn(1); setUpSurnameEditor(table, surnameColumn); //Add the scroll pane to this window. setContentPane(scrollPane); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } private void setUpSurnameEditor(JTable table, TableColumn column) { //Set up the editor for the Sport cells. final Object[] data = {"Andrews", "Buena", "Campione", "Crawford", "Dougherty", "Grady", "Huml", "Kringle", "Lih", "McNabb", "Vaughn", "Walrath", "Witty"}; final JButton button = new JButton(""); button.setBackground(Color.white); button.setBorderPainted(false); button.setMargin(new Insets(0,0,0,0)); //button.setFont(/* XXX: how do I get this? */); final SurnameEditor editor = new SurnameEditor(button); column.setCellEditor(editor); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { button.setText(editor.currentValue); TableDialog.showDialog(button, editor.currentValue, data, "Surname Editor"); button.setText(TableDialog.getValue()); } }); } private void setUpIntegerEditor(JTable table) { //Set up the editor for the integer cells. final WholeNumberField integerField = new WholeNumberField(0, 5); integerField.setHorizontalAlignment(WholeNumberField.RIGHT); DefaultCellEditor integerEditor = new DefaultCellEditor(integerField) { //Override DefaultCellEditor's getCellEditorValue method //to return an Integer, not a String: public Object getCellEditorValue() { return new Integer(integerField.getValue()); } }; table.setDefaultEditor(Integer.class, integerEditor); } /* * The editor button that brings up the dialog. * We extend DefaultCellEditor for convenience, * even though it mean we have to create a dummy * check box. Another approach would be to copy * the implementation of TableCellEditor methods * from the source code for DefaultCellEditor. */ class SurnameEditor extends DefaultCellEditor { int currentRow = -1; int currentColumn = -1; String currentValue = ""; public SurnameEditor(JButton b) { super(new JCheckBox()); //Unfortunately, the constructor //expects a check box, combo box, //or text field. editorComponent = b; setClickCountToStart(2); //Must do this so that editing stops when appropriate. b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fireEditingStopped(); } }); } protected void fireEditingStopped() { super.fireEditingStopped(); } public Object getCellEditorValue() { System.out.println("TableDialog value: " + TableDialog.getValue()); currentValue = TableDialog.getValue(); return currentValue; } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { ((JButton)editorComponent).setText(value.toString()); currentRow = row; currentColumn = column; currentValue = (String)value; return editorComponent; } } class MyTableModel extends AbstractTableModel { final String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; final Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)}, {"Mark", "Andrews", "Speed reading", new Integer(20), new Boolean(true)}, {"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)} }; 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 < 1) { return false; } else { return true; } } public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } data[row][col] = value; if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + data[i][j]); } System.out.println(); } System.out.println("--------------------------"); } } public static void main(String[] args) { TableDialogEditDemo frame = new TableDialogEditDemo(); frame.pack(); frame.setVisible(true); } }