Cell Editors


Overview Basic version Model Cell Renderer Editor Column widths Tooltips Controls in cells

When the user starts editing a cell, instead of being drawn by the cell renderer (default JLabel), it is replaced by a component suitable for an editor - default JTextField.

As for the renderer, we can define different editors for different data types in cells. There are suitable default editors for the standard types, or you can define your own.

Your custom editor should be a subclass or AbstractCellEditor, and implement the TableCellEditor interface (otherwise you can't set it as a table editor). You need to code two methods - getTableCellEditorComponent, which returns the component used for the editing, and getCellEditorValue, which hands a value back at the end of the editing process.

If you want to validate the value entered, you could do it in the getCellEditorValue method.

For example, we will produce this:

to edit a date in a format we want. The table model has just two columns, a String and a Date type.

The data model is:

 
class MyModel extends AbstractTableModel {

    private String[] columnNames = {"Name", "DoB"};
    private Object[] names = {"John", "Joe", "Julian"};
    // use a deprecated constructor for simplicity..
    private Date[] dob = {new Date(80, 5, 5), new Date(82, 6, 6), 
    new Date(83, 4, 4)};

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return names.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        if (col == 1) {
            return dob[row];
        }
        else return names[row];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        if (col == 1) {
            dob[row] = (Date) value;
        } else {
            names[row] = value;
        }
        fireTableCellUpdated(row, col);
    }
}

The editor including validation is


    class MyDateEditor extends AbstractCellEditor  implements TableCellEditor {

    Date currentDate;
    JTextField tf;
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");

    MyDateEditor() {
        tf = new JTextField(); // format how you like..
        tf.setBorder(BorderFactory.createLineBorder(Color.red));
    }

    @Override
    public Object getCellEditorValue() {
        Date d = null;
        String s = tf.getText(); // get text entered
        try {
            d = df.parse(s); // try to convert to date
        } catch (ParseException ex) { //if invalid
            JOptionPane.showMessageDialog(null, s + "is not a valid date");
            d = currentDate; // revert to previous value
        }
        return d;
    }

    //The TableCellEditor interface method.
    public Component getTableCellEditorComponent(JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column) {

        currentDate = (Date) value; // remember current date
        String s = df.format(currentDate); // change to text
        tf.setText(s); // put text in textfield
        return tf; // this is our editor component
    }
}

            

We set this as the editor for Date type..


        MyModel data = new MyModel();
        table = new JTable(data);
        table.setDefaultEditor(Date.class, new MyDateEditor());

            

Comments

0 responses so far

Add a new comment

Your name

Your email (will not be published)

Your comment (no HTML)

Are you a robot? Please type the two words: