What is the JTable CTRL+C event's name?

时光毁灭记忆、已成空白 提交于 2019-11-27 08:41:20

问题


I am developing a Java application, and when I press CTRL+C on a jTable, I can get the clipboard and paste it in Excel. I would like to implement a button that does the same thing. How can I get the function, listener, whatever it is that I can use to achieve this?

PS: I have tried looking at other questions but none seem to be looking for what I want.


回答1:


The key for the table's copy action is "copy":

Action copyAction = table.getActionMap().get("copy");

But I don't see a useful way to recycle the Action:

JButton button = new JButton(copyAction);

Instead, just export the table's current selection to the system clipboard.

JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new DefaultTableModel(
    new Object[][]{{"Some"}, {"More"}}, new Object[]{"Name"});
final JTable table = new JTable(model);
table.getSelectionModel().setSelectionInterval(0, 1);
f.add(table);
f.add(new JButton(new AbstractAction("Export") {

    @Override
    public void actionPerformed(ActionEvent e) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        table.getTransferHandler().exportToClipboard(
            table, clipboard, TransferHandler.COPY);
        Transferable contents = clipboard.getContents(null);
    }
}), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);

Addendum: This variation relies on TableTransferable.

final DefaultTableModel model = new DefaultTableModel(
    new Object[][]{
    {"A1", "A2", "A3", "A4", "A5"},
    {"B1", "B2", "B3", "B4", "B5"},
    {"C1", "C2", "C3", "C4", "C5"},
    {"D1", "D2", "D3", "D4", "D5"},
    {"E1", "E2", "E3", "E4", "E5"},
    {"F1", "F2", "F3", "F4", "F5"}
},
    new Object[]{"1", "2", "3", "4", "5"});
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTable table = new JTable(model);
table.getSelectionModel().setSelectionInterval(0, 1);
f.add(table);
f.add(new JButton(new AbstractAction("Export") {
    @Override
    public void actionPerformed(ActionEvent e) {
        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        cb.setContents(new TableTransferable(model), new ClipboardOwner() {
            @Override
            public void lostOwnership(Clipboard clipboard, Transferable contents) {
                System.out.println("Clipboard lost!");
            }
        });
    }
}), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);



回答2:


But I don't see a useful way to recycle the Action:

You can't use the Action in that way because the source of the Action is the button not the table.

See Action Map Action for a general solution. Using the supplied class the code would be:

Action copyAction = new ActionMapAction("Copy Table", table, "copy");
JButton copyButton = new JButton(copyAction);


来源:https://stackoverflow.com/questions/14356859/what-is-the-jtable-ctrlc-events-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!