Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable & custom cell editor

794041Jan 19 2009 — edited May 11 2009
Hello everyone,
what is the correct way of writing a custom cell editor for a JTable? I followed the example in the Java tutorial ([How to use tables|http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editor]), but the result is a bit weird. The code I have is the following:
    private class NumericCellEditor extends AbstractCellEditor implements TableCellEditor {

        NumericFTField field = new NumericFTField(threeDecimalsFormat, 3, null, 1);

        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int col) {
            field.setValue(value);
            return field;
        }

        public Object getCellEditorValue() {
            return field.getValue();
        }

        @Override
        public boolean stopCellEditing() {
            if (((NumericFTField)field).verifyDouble()) {
                field.setBorder(new EmptyBorder(0, 0, 0, 0));
                fireEditingStopped();
                return true;
            } else {
                field.setBorder(BorderFactory.createLineBorder(Color.red));
                return false;
            }
        }
    }
where the NumericFTField is a class derived from JFormattedTextField that only allows digits, decimal separator, minus and 'E' to be inserted, and it monitors clipboard operations. verifyDouble() is a method of the NumericFTField class that verifies whether the current input can be parsed to a double and whether it satisfies everything it should. This is then used in
((TableColumn)jTblSpecs.getColumnModel().getColumn(1)).setCellEditor(new NumericCellEditor());
The NumericFTField class works great, I use it also in place of a JTextFields, so I'd say there is nothing wrong with it.

After I click in a cell (single click), it behaves a little different that the default cell editor: the cell is not highlighted, but it immediately jumps to the editing state (why???). I, indeed, can insert the allowed characters only. When I click in a cell, do some editing and press Enter, the cell's content gets validated. If it is invalid, stopCellEditing() method does its magic; if it is valid, the caret disappears and everything SEEMS okay. However, if I started typing at this point, the cell reverts to the editing state, but now I am able to enter any character I want. It truly looks like the cell editor is now some other component, not the original NumericFTField one. What is going on here?

It would be great is someone could provide a short schematic source of a custom cell editor class that would work exactly as the JTable's default one except it would only permit digits and so on. It doesn't have to be anything fancy, just a "skeleton" of the class with comments like "input verification here" etc.

I am sorry for any lack of clarity, but I am still a Java newbie.

Any help would be much appreciated.

Best regards,
vt
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2009
Added on Jan 19 2009
1 comment
248 views