• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable problem

 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a JTable.. When we select one particular row in it and press Enter I want to get hold of the row that has been selected... I added a Key Listener to my table and looking for Enter key.

The moment when the Enter key is pressed and in my listener when I call m_table. getSelectedRow(), it gives me the next row. Like when we select row '1' and press enter, table.getSelectedRow() gives '2'.

In my key Listener i override keyReleased to catch the Enter Event.

Any help on this ASAP.
 
Maneesh Godbole
Saloon Keeper
Posts: 11061
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karthick Dharani Vidhya:

Any help on this ASAP.


Ease Up.

This is the default table behavior. If you have observed the row jumping is cyclic. If you hit enter when the last row is selected, it will select the first row.

Now since you know it jumps a row, you can either call the getSelectedRow and subtract 1 from it or, trap the keyPressed event instead of the keyReleased as the keyPressed will give you the correct row selection.

PS. There might be a solution in playing around with key bindings. It is up to you to decide if it is worth the effort.
[ August 05, 2008: Message edited by: Maneesh Godbole ]
 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was able to do it yest. Sorry forgot to update.

I did the same thing like getting the selectedRow from table and sub 1 from that.

But this sounded bit buggy. One more way is to override KeyPressed instead of keyReleased. When we do this, getSelectedRow gives you the row where we pressed enter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic