Hi, I've a JTable where the first column contains an extension of a
JFormattedTextField.
The field is set to only allow the user to type in dates in the format
MM/DD/YYYY i.e. a blank field is displayed as __/__/____, when the user
types it replaces the characters where the underscores are and skips over
the / chars.
This works fine if I double click on the cell and start typing. It also
works fine as a straightforward text field if I select the field or tab onto
the field and start typing.
If I tab onto the cell in the JTable (either from the component before the
JTable or from another cell within it) and start typing, the first character
gets entered into the field before any of the text field verification kicks
in i.e. the key event doesn't seem to get sent to the processKey method in
the text field for verification allowing the field to become 11 chars long
which then screws up further validation.
I've tried various methods of capturing the focus or key events from the
JTable and passing them to the textfield, trying to make the cell go into
editiing mode when it's tabbed onto, altering JTable config such as
setSurrendersFocus etc. but just can't seem to get the combination right.
Anyone know what I should be aiming for here?
Cheers, Richard.
zero - 10 Feb 2006 13:01 GMT
> Hi, I've a JTable where the first column contains an extension of a
> JFormattedTextField.
[quoted text clipped - 24 lines]
>
> Cheers, Richard.
How did you set the JFormattedTextField as editor? The default
implementation brings up the editor when you double click, as you say.
I also believe (can't test it right now though) that the cell enters
editing mode when you start typing while the cell has focus. So
basically, it has very little to do with getting the focus, and it will
be hard to fix by only handling focus events.
You have different options availalbe: don't start the editor when you
type in the cell (only editing it after double clicking), start the
editor as soon as the cell has focus, or try to emulate the default
behaviour.
The first option can be set (again, can't test right now) by using
JComponent:putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
For the second option, you could try adding a ListSelectionListener to
JTable:getSelectionModel(). Then when capturing
ListSelectionListener:valueChanged(), call JTable:editCellAt()
The third option may require overriding JTable:processKeyBinding(), but
I'm not sure how you would go about that.