Reputation: 2309
I am writing a swing application with a menu, a toolbar and a table.
I have mapped various key strokes like CTRL + W to specific actions. These all work fine, except for the CTRL + V, CTRL + C and CTRL + X. I want to be able to cut or copy rows and then paste them in the table.
The functionality itself works fine when i click the buttons, but using the keyboard shortcuts will not fire these 3 specific events. Is it possible that the JTable is capturing them by default? And if so, how can i disable this behaviour?
Action declaration:
ExampleAction editCopy = new ExampleAction();
editCopy.putValue(Action.NAME, "Copy");
editCopy.putValue(Action.SMALL_ICON, ClientUtil.getImageResource("copy.gif"));
editCopy.putValue(Action.SHORT_DESCRIPTION, "Copy the selected row(s)");
editCopy.putValue(Action.MNEMONIC_KEY, new Integer('C'));
editCopy.putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
Adding the action to the menu and toolbar:
JMenu menu = new JMenu();
JMenuItem menuItem = new JMenuItem(editCopy);
KeyStroke accKey = (KeyStroke) editCopy.getValue(Action.ACCELERATOR_KEY);
menuItem.setAccelerator(accKey);
menu.add(menuItem);
JToolBar toolbar = new JToolBar();
JButton button = new JButton(editCopy);
toolbar.setText("");
toolbar.add(button);
I didn't do anything special with the JTable.
Upvotes: 2
Views: 1086
Reputation: 51525
An outline of the deeper solution:
Those keys are the default paste/copy/cut strokes, which are already bound in the table's action/inputMap to actions provided by the TableTransferHandler which ... paste/copy/cut :-).
As you want to implement those action, the task is two-fold:
Below is the original, which got accepted ;-)
The part of the table (same for tree, list) that's capturing the copy/paste/cut bindings is the TransferHandler that's installed by their ui-delegates.
A quick solution that comes to my mind is to null the handler:
table.setTransferHandler(null);
A deeper solution would try to hook into the transferHandler, see above.
Upvotes: 4