gpt4 book ai didi

java - 如何从 JButton 外部调用 JTable Action ?

转载 作者:行者123 更新时间:2023-11-29 03:32:19 25 4
gpt4 key购买 nike

我有 JTableTransferHandler,它允许通过 hotkeys 进行copy-cut-paste 操作>DnD。我在 JTable 之外有 3 个 JButtons(copy-cut-paste),它们应该在 JTable 的 TransferHandler< 上调用类似的操作(例如 canImport() 等)。

我该怎么做?

最佳答案

基本上与 recent question/answer 中的方法非常相似: 在其 actionMap 中找到表的复制操作,将其包装成委托(delegate)给原始操作的自定义操作,并在按钮中使用自定义操作:

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

关于java - 如何从 JButton 外部调用 JTable Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17589304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com