File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes KeyEvent handling in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyEvent handling in a JTable" Watch "KeyEvent handling in a JTable" New topic

KeyEvent handling in a JTable

Rafe Thayer

Joined: Oct 20, 2002
Posts: 2
Howdy folks!
I'm trying to implement a JTable that automatically adds and deletes rows as the user types. The last row will always be blank (except for data in the 1st column), so when a user types something in any column of the last row, I'd like it to add a new blank row to the bottom of the table. In addition, I'd like rows that become empty from a user pressing backspace or delete to automatically disappear.
What I've written so far almost works, but ocassionally when a new row gets added, the characters that the user typed disappear. Deletion doesn't work predictably either. Here's the class:

Thanks for your help!
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
the values typed by the user disappear when editing is cancelled. this is a bug in JTable:
these bugs come up when the cell is still in editing mode, and the table loses focus. but it can happen even when just another cell is selected (through keyboard action). I didn't find the bug ids in the bugparade at once.
it _seems_ as this is fixed in 1.4.1.
what concerns the deletion of the row which is not propagated to the view (= JTable). Be sure, your tablemodel fires an event on row deletion.

Rafe Thayer

Joined: Oct 20, 2002
Posts: 2
Thanks for the help Chantal. Finally got it working reasonably. The problem was that I didn't set an appropriate default cell editor. Also, FYI, when the cell editor cancels editing, the data still disappears. In my case, the cell loses focus when a new row is added (which is after the first character gets typed in the nth cell). Does anyone know offhand how to do a fireTableRowsInserted(x,y) without changing the currently selected and editing cell?
Here's what the code looks like now:
I agree. Here's the link:
subject: KeyEvent handling in a JTable
It's not a secret anymore!