Apply an Action from something else (i.e. a JMenuItem) to a JButton. Swing is so absof ckinglutely convoluted sometimes. Do we really need half a dozen lines of boilerplate to apply a key shortcut to a button?
(Action action, JButton button)
| 253 | * half a dozen lines of boilerplate to apply a key shortcut to a button? |
| 254 | */ |
| 255 | static public void applyAction(Action action, JButton button) { |
| 256 | button.setAction(action); |
| 257 | // use an arbitrary but unique name |
| 258 | String name = String.valueOf(action.hashCode()); |
| 259 | button.getActionMap().put(name, action); |
| 260 | button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) |
| 261 | .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), name); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /** |
no test coverage detected