It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Getting Focus in my JTable cell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting Focus in my JTable cell" Watch "Getting Focus in my JTable cell" New topic

Getting Focus in my JTable cell

Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 214
I am wanting my table cells to stop editing when I click outside the table.

I made a custom editor and tried to close/cancel editing when the focus shifts to some other panel like this:

If I double-click in a table cell, I see the comforting message "Focus gained" and then if I type in a number and click outside the GUI, for example, I get "Lost focus editing cell" and the cell editing finishes. Exactly what I want.

If I just type a number, the cell contains the number and looks to be in edit mode. Though I do not see the message "Focused gained" and if I click outside the GUI, I still seem to be in edit mode. To get out of it, I have to click another cell in the table.

I tried putting this in my table model:

I see the message "Requesting Focus." The first println returns true, the second false. So again, clicking outside the table fails to get the CellEditor to stopCellEditing. No surprise, as I seemed to have failed to get focus in the cell.

Could someone help me with this?

How should I be requesting focus on the editing text field?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

To stop editing a cell when it loses focus, use the following code:
This is one of those non-documented features that everybody wants to know about.

How To Ask Questions How To Answer Questions
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 214
Thanks for the suggestion. I did come across that property myself last night, but it produced an interesting dilemma. I had the table set up to more 'Excel-like' in that you can start typing in a cell and the new value will replace the old value, rather than having the new text appended to what was already in the field. Users also like that they can use right and left arrow keys to move between cells in the table.

At least in my hands, 'table.putClientProperty("terminateEditOnFocusLost", true);' breaks all that functionality.

There is more than one method I have seen to implement the Excel behavior. They override functions of JTable.

overriding editCellAt and selectAll() seems to work also.

both recipes come from trolling with Google.

I can't take away functionality users really like, but they complain a lot about leaving the table and not having it update.

What I read about setting the property made me think as a minimum, I'd always end up in the editor when typing new text, so I could not use the right and left arrows for navigation. I have been thinking I'd be better off trying to attach another listener somewhere to call stopCellEditing(). But if there are ways to keep the 'Excel-ness' and use 'terminateEditOnFocusLost' I would love to hear suggestions.
I agree. Here's the link:
subject: Getting Focus in my JTable cell
It's not a secret anymore!