添加复制快捷键将动作粘贴到所有 Swing 组件 [英] Add shortcut keys for copy & paste actions to all Swing components
问题描述
我经历了这个问题 Java - 如何在 Mac 中使用 Command+A 快捷键选择 JTable 中的所有行? 关于如何为 Mac 添加快捷键.我想知道我是否可以使用它来将这些快捷键添加到所有组件,或者我是否必须为每个组件进行如下设置:
I went through this question Java - How can i select all rows in JTable using Command+A shortcut in Mac? on how to add shortcut keys for Mac. I want to know if I can use this to add these shortcut keys to all components or do I have to set for each component like below:
For JTABLE:
InputMap im = myTable.getInputMap( JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
final int CMD_BTN = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_A, CMD_BTN ), "selectAll" );
推荐答案
每个 Swing 组件都有一个父 InputMap.所以你应该能够改变那个 InputMap 并且绑定应该适用于该类型的所有实例.例如,为您将使用的所有 JTable 实例添加绑定:
Each Swing component has a parent InputMap. So you should be able to change that InputMap and the binding should work for all instances of that type. For example to add a binding for all JTable instances you would use:
InputMap im = (InputMap)UIManager.get("Table.ancestorInputMap");
对于 JTextArea,您将使用:
And for a JTextArea you would use:
InputMap im = (InputMap)UIManager.get("TextArea.focusInputMap");
要查看每个组件使用哪个 InputMap,请查看:UIManager Defaults
To see which InputMap is used by each component check out: UIManager Defaults
这篇关于添加复制快捷键将动作粘贴到所有 Swing 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!