Home > Software design >  JTable with JComboBox navigation and selection using keyboard only
JTable with JComboBox navigation and selection using keyboard only

Time:01-20

I'd like to navigate on JTable using only a keyboard, and select values from JComboBox cells also only with a keyboard.

I'm using Oracle Java JTable tutorial, the code is here: https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java

table

My question is how to select items from combo box when navigating only with the keyboard, for example:

  1. I execute the mentioned demo, the table initially is out of focus
  2. I press 'tab' on the keyboard and the table receives focus, selects the first row and the leftmost column, then pressing 'tab' twice the focus moves to 'Sport' column which has JComboBox as cell editor
  3. Here is what I want to achieve: while focus is on 'Sport' column, I'd like to press for example 'r' letter on the keyboard to change a value for Kathy's sport to 'Rowing' (which is in the list). But the only thing happens when I press 'r' while on that cell is the cell renderer changes to JComboBox, but does not select any desired value from it despite I continue to press various keys on the keyboard.

JComboBox appears

How to achieve this functionality?

CodePudding user response:

After more googling I found what I think I needed, here is a link to thread and example from oracle.com oracle.com

Basically a custom ComboBoxEditor which extends DefaultCellEditor solves the issues. Looks like the default one has several bugs.

  • Related