• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tabbing out of a JTable

 
Henry Carver
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have what I thought was a fairly simple issue. I have a JTable in a window and below the JTable are an OK and Cancel button. What I would like to do is be able to tab through the entries in the JTable but when I get to the last entry I would like to tab out of the JTable and give focus to the okButton. This is not the default behavior of the JTable because the default is to have the tab go back to the beginning of the JTable. You need to use the Ctrl+Tab to get out of the JTable. I thought, no big deal, I'll just catch the key stroke and if the tab is pressed on the last entry, I'll set focus to the OK Button.

Here is the simplified code. You can pretend that there is only one column in each row.



Easy right? And it works like a charm... most of the time. If you are scrolling through the JTable and even editing entries, it works perfectly. The problem is if you double click on the last row and then tab. In that case, the key stroke isn't getting to the JTable and the focus ends up at the first row (the default behavior). I am assuming that the key stroke is getting to the underlying JTextField in the JTable and is getting processed there so I never see it in the JTable. My question is, how can I catch that key stroke and if I can, how can I determine that I am at the last row of the JTable so I can know to give focus to the okButton?

Thanks for any help.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming that the key stroke is getting to the underlying JTextField


Correct, the editor component (whatever that may be) has focus.

My question is, how can I catch that key stroke


Don't use a KeyListener!

Swing works by binding KeyStrokes to Actions. So what you really want to do is customize the Tab Action so that when you are at the end of the table you just tab out of the table. This should be easy enough to do in the custom Action you should be able to just do:



My suggestion for creating the custom Action is to follow the approach used in Table Tabbing
 
Henry Carver
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friggin' fantastic!!! I did a quick test and it works perfectly. Thanks so much!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!