wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes JTable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable problem" Watch "JTable problem" New topic

JTable problem

Karthick Dharani Vidhya
Ranch Hand

Joined: Feb 23, 2008
Posts: 91

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.

Karthick D V
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10767

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 ]

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

Joined: Feb 23, 2008
Posts: 91

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: JTable problem
It's not a secret anymore!