简体   繁体   中英

JTable with JComboBox navigation and selection using keyboard only

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

桌子

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出现

How to achieve this functionality?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM