我有一个非常简单的 GUI,在 SplitPane 中使用 2 个 JTable,我想使用 TAB 删除一个 JTable 中的选择并移动焦点并选择另一个 JTable 的第一行。 gui 只是其自己的 ScrollPane 中的每个 JTable。 SplitPane 和 SplitPane 中的 ScrollPanes 都在我的类中,它扩展了 JFrame。
我的做法是:
table_LEFT.getInputMap().put(KeyStroke.getKeyStroke("TAB"),
"doSomething");
table_LEFT.getActionMap().put("doSomething",
new myAction(table_RIGHT));
这会将键 TAB 注册到扩展 AbstractAction 的 myAction。在 AbstractAction 中,我必须重写“actionPerformed(ActionEven ae)”方法。问题是我无法访问另一个表(在本例中是 ae 事件中的“table_RIGHT”)。我必须遍历容器到顶部,其中 myClass 的属性是另一个表。这看起来非常麻烦且错误 + 加我设法在 table_RIGHT 中选择一行,但不给它焦点...。我这样做对吗?
最佳答案
I have to override the 'actionPerformed(ActionEven ae)' method.
正确。
The problem is I cannot access the other table (in this case the 'table_RIGHT' from the ae event.
当您创建操作时,您始终可以在创建操作时传递对右侧表的引用。然后存储该引用,以便可以在 actionPerformed(...) 方法中访问它。
plus I managed to select a row in table_RIGHT, but don't give it focus ... .
如果您有引用,就没有理由不能将焦点放在表格上。基本代码是:
rightTable.requestFocustInWindow();
rightTable.changeSelection(...)
如果您需要更多帮助,请发布您的 SSCCE这说明了问题。
关于java - 使用 TAB 更改 JSplitPane 中的选择和焦点(从一个 JTable 到另一个 JTable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322534/