This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyEvent handling in a JTable

 
Rafe Thayer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Rafe
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
the values typed by the user disappear when editing is cancelled. this is a bug in JTable:
http://developer.java.sun.com/developer/bugParade/bugs/4330950.html
http://developer.java.sun.com/developer/bugParade/bugs/4709394.html
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.

chantal
 
Rafe Thayer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic