简体   繁体   English

JTable 仅使用键盘进行 JComboBox 导航和选择

[英]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.我想仅使用键盘在 JTable 上导航,而 JComboBox 单元格中的 select 值也仅使用键盘。

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我正在使用 Oracle Java JTable 教程,代码在这里:https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src24Demo89.components/TableRenderDemoProject/src13588.6

桌子

My question is how to select items from combo box when navigating only with the keyboard, for example:我的问题是仅使用键盘导航时如何从组合框中获取 select 项,例如:

  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我在键盘上按“tab”,表格获得焦点,选择第一行和最左边的列,然后按两次“tab”,焦点移动到“Sport”列,其中 JComboBox 作为单元格编辑器
  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).这是我想要实现的目标:虽然焦点在“运动”列上,但我想按例如键盘上的“r”字母将凯西运动的值更改为“划船”(在列表中) . 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.但是,当我在该单元格上按“r”时,唯一发生的事情是单元格渲染器更改为 JComboBox,但没有 select 任何所需的值,尽管我继续按键盘上的各种键。

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经过更多谷歌搜索后,我找到了我认为我需要的东西,这里是线程和示例的链接,来自 oracle.com oracle.com

Basically a custom ComboBoxEditor which extends DefaultCellEditor solves the issues.基本上,扩展 DefaultCellEditor 的自定义 ComboBoxEditor 解决了这些问题。 Looks like the default one has several bugs.看起来默认的有几个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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