Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes Jtable cell editing question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtable cell editing question" Watch "Jtable cell editing question" New topic

Jtable cell editing question

Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142

I've run into some problem with JTable. All of my cells are editable, and I would like to provide an easy way to edit cells. First I used single click as the edit mode activator, but that is not too good, since whenever a user selects a row, it immediatlly starts to edit that cell. I also tried double click, but that method not too intuitive, and you have to make a lot of extra move. Is it possible to achieve somehow to start editing on the first click, but only if the row is already selected(not selecting by the click)?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I've run into some problem with JTable.

Really? I can't imagine... ;) JTable is always a problem. At least for me. Powerful, but difficult.

Off the cuff, you would need to determine this in your table model's isCellEditable method. So in that method you would need logic to determine if the selected cell is part of an existing selected row. The problem with this is going to be timing. Does the row get selected before or after this method gets called? If it happens before, then your logic will always be true, which is a problem.

GenRocket - Experts at Building Test Data
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10747

Miklos Szeles wrote:..., but that method not too intuitive, and you have to make a lot of extra move.

That is one of the reasons why allowing the user to directly edit in the table is bad usability.
I always prefer a popup menu with a clearly defined "Properties" or "Edit" option. Invoking this action displays the user with a dialog with the editable fields.

With a non editable table, a JPopupMenu and JOptionPane you can make a user friendly and intuitive UI.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Thanks for the answers. I solved the problem. I have a common base class for all my cell editors and I overriden the isCellEditable method of the editor in which I compare the table's selected row and the row under the mouse.
I agree. Here's the link:
subject: Jtable cell editing question
It's not a secret anymore!