2

我正在编写一个带有菜单、工具栏和表格的摇摆应用程序。

我已经将各种击键(如CTRL+ )映射W到特定操作。这些都可以正常工作,除了CTRL+ VCTRL+CCTRL+ X。我希望能够剪切或复制行,然后将它们粘贴到表格中。

当我单击按钮时,功能本身可以正常工作,但使用键盘快捷键不会触发这 3 个特定事件。JTable 是否有可能默认捕获它们?如果是这样,我该如何禁用这种行为?

动作声明:

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

将操作添加到菜单和工具栏:

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

我没有对 JTable 做任何特别的事情。

4

2 回答 2

4

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:

  • implement and set a custom TransferHandler which support those actions (see the tutorial chapter on Swing dnd)
  • wrap your menu actions around the transferHandler's action: the wrapper would provide the visuals like f.i. text, icon and delegate the actual actionPerformed to the handler action

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.

于 2013-06-24T13:53:39.650 回答
3

我会使用键绑定作为键组合。

于 2013-06-24T13:39:52.830 回答