0

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" );
wazza
  • 47
  • 9

1 Answers1

1

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");

And for a JTextArea you would use:

InputMap im = (InputMap)UIManager.get("TextArea.focusInputMap");

To see which InputMap is used by each component check out: UIManager Defaults

camickr
  • 321,443
  • 19
  • 166
  • 288
  • So I have to set the input map for each binding, there's no other way to set the input map for all components? – wazza Mar 17 '20 at 14:57
  • I thought I just answered your question. If I new how to do it for all different types of components in a single statement I would have given that answer. I don't. So I gave you the next best option that I know of. Besides not all components have a "selectAll" Action, so implementing this approach should be relatively simple. – camickr Mar 17 '20 at 15:02
  • I have to do this for copy/paste actions too. As of now your approach is the best I guess. Any way I can do this by tweaking the L&F. Just asking – wazza Mar 17 '20 at 15:09