1

我有JTablewith TransferHandlerwhich 允许通过热键DnD进行复制-剪切-粘贴操作。我有 3之外的(复制剪切粘贴),它应该调用类似的操作(例如等)。JButtonsJTableJTable's TransferHandlercanImport()

我怎样才能做到这一点?

4

2 回答 2

2

基本上与最近的问题/答案非常相似:在其 actionMap 中找到表的复制操作,将其包装到委托给原始操作的自定义操作中,并在按钮中使用自定义操作:

table.setDragEnabled(true);
final Action copy = table.getActionMap().get("copy");
Action copyWithButton = new AbstractAction("copy") {

    @Override
    public void actionPerformed(ActionEvent e) {
        copy.actionPerformed(
                new ActionEvent(table, e.getID(), e.getActionCommand()));
    }
};
frame.add(new JScrollPane(table));
frame.add(new JButton(copyWithButton), BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(5, 20)), BorderLayout.SOUTH);
于 2013-07-11T09:49:26.883 回答
2

谢谢大家,但我在等待答案时自己找到了答案:

private void onAction(String actionStr) {
    Action action = table.getActionMap().get(actionStr);
    ActionEvent newAE = new ActionEvent(table, ActionEvent.ACTION_PERFOMED, actionStr);
    action.actionPerfomed(newAE);
}

private void decorateButtons() {
    copyButton.addActionListener(new ActionListener() {
        public void actionPerfomed(ActionEvent ae) {
            onAction("copy");
        }
    });
    cutButton.addActionListener(new ActionListener() {
        public void actionPerfomed(ActionEvent ae) {
            onAction("cut");
        }
    });
    pasteButton.addActionListener(new ActionListener() {
        public void actionPerfomed(ActionEvent ae) {
            onAction("paste");
        }
    });
}
于 2013-07-11T10:00:37.543 回答