So, my table consists of multiple rows and columns, some editable and
some not, and a few different CellEditors (JComboBoxes and
JFormattedTextFields). In addition the table is part of a JPanel with
another panel of JRadioButtons. I would like the "intuitive" (client's
words, not mine) functionality to be able to Tab across cells and
start typing to edit the fields, as well as to double-click on a cell
to start editing.
I have tried the KeyStroke thing and it doesn't work, I'm guessing
because when the table is reinitialised the KeyEvent and focus is
lost. I cannibalised the code and inserted it into the
reinitialisation code to reselect the previously selected cell before
reinitialisation, but this seems to work only under one condition -
that the user DIDN'T double-click on a cell to start editing. And it
doesn't actually work properly - the focus moves first through the
radio buttons on top of the table before going to the next cell in the
table.
I guess there's a few questions here - why is it that if I double-
click a cell to edit, the "Tab" KeyEvent seems to be lost (and the
Action written for it is never reached), whereas if I single-click to
select then start typing then "Tab" it works? And this
reinitialisation probably ruins the focus traversal order, but how do
I get it to go straight to the next cell after reinitialisation, and
not to the radio buttons above the table? Reinitialisation should be
completely invisible to the user.
I hope you understood what I just typed. I'd be grateful for any help,
it's driving me nuts.
How does your table model handle changes to it? Usually you don't need to reload
and revalidate everything but only changed values. Use the tableChanged method
in the TableModelListener to tell the listener exactly what has changed.
>
> So, my table consists of multiple rows and columns, some editable and
> some not, and a few different CellEditors (JComboBoxes and
> JFormattedTextFields). In addition the table is part of a JPanel with
> another panel of JRadioButtons. I would like the "intuitive" (client's
> words, not mine) functionality to be able to Tab across cells and
> start typing to edit the fields, as well as to double-click on a cell
> to start editing.
The thing is your TAB KeyStroke only works as long as the actual table is in
focus. When you start editing the editing field (JTextField or whatever) is in
focus.
>
>...
Regards
Stefan
Yes, my implementation is unusual. It is because there is a graphical
representation of the data in the table that exists alongside and any
update to either the graphical or the table representation results in
an update of all representations as any change in some particular data
may influence other data and there is no way at present to predict
when this may occur. Therefore I've elected just to update everything
on any change.
The table model stores the data in an internal data structure.
> The thing is your TAB KeyStroke only works as long as the actual table is in
> focus. When you start editing the editing field (JTextField or whatever) is in
> focus.
So how can I get the table back in focus after editing AND reloading?
I may have gone partway there - as I said before after reloading I
programmatically set the correct selected cell (the next one along).
Now when I double-click to edit it and then press TAB the focus seems
to be gone again, but if I navigate to another program's window and
then come back, the focus is correctly on the cell! If I single-click
to select then start typing then press TAB it exhibits the behaviour I
mentioned earlier, namely the focus goes to the buttons first before
coming to the correct cell in the table.
Thanks for your reply!