FileDocCategorySizeDatePackage
TableEditDemo.javaAPI DocExample5080Tue Dec 12 18:59:16 GMT 2000None

TableEditDemo

public class TableEditDemo extends JFrame
This is exactly like TableDemo, except that it uses a custom cell editor to validate integer input.

Fields Summary
private boolean
DEBUG
Constructors Summary
public TableEditDemo()


      
        super("TableEditDemo");

        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);

        //Add the scroll pane to this window.
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    
Methods Summary
public static voidmain(java.lang.String[] args)

        TableEditDemo frame = new TableEditDemo();
        frame.pack();
        frame.setVisible(true);
    
private voidsetUpIntegerEditor(javax.swing.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);