Focus Traversal In JTable Using TAB Key

Focus traversal from cell to cell in a JTable seems to be a very interesting topic. I scoured the internet looking for a solution and it was not easy. People suggested about focus traversal among others which did not work. One person in a forum mentioned overriding the changeSelection method of the JTable that made it work.

That’s it. Whenever you press the TAB key if the JTable has focus, the focus will transfer to the next cell. If your cell has an editor associated to it and if the editor returned is a container like a JPanel, you can modify code in your cell editor class and add a FocusListener to the panel object while adding code in the focusGained() method and call requestFocusInWindow() to the Swing component that you want to receive focus.

Related Posts Plugin for WordPress, Blogger...

3 comments

  1. Overriding changeSelection int hsi way can break mouse clicking a cell. The reason is that the editor is invoked when clicking the cell. Then changeSelection is called by the JTable class. Then it calls editCellAt a second time. So if the editing cell was a JComboBox, the popup would show for a millisecond or two then disappear again. The better approach that doesn’t break mouse handling, and gives the desire keyboard focus traversal is to provide a custom action for the keys that you want to use for navigation, and in the actionPerformed method of that action call editCellAt / requestFocusInWindow per the original suggestion.

Leave a Reply

Your email address will not be published. Required fields are marked *