1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Oliver Hoffman
  • 540
  • 1
  • 9
  • 22
  • Do you want the whole table to be copied on the clipboard or some specific values? Have you tried some code? if yes please post the code. – Nitesh Verma Jan 16 '13 at 11:08
  • Are you trying to provide copy/paste functionality for JTable cells? – Waleed Almadanat Jan 16 '13 at 11:27
  • 1
    You really should Google questions likes this - the search 'JTable cut copy paste' turned this up - http://www.javaworld.com/javatips/jw-javatip77.html which I suspect answers your question. – Nick Holt Jan 16 '13 at 11:29

2 Answers2

4

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);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Since you bring it up -- I'm curious -- why doesn't recycling the action work? – Mike Clark Jan 16 '13 at 17:25
  • 1
    Good question; internally, the L&F delegates to the `TransferHandler`, so nothing appears to happen until the export. – trashgod Jan 16 '13 at 17:43
  • 1
    Recycling the Action doesn't work in this case because the source component is the button, not the table. I provided an alternative answer to solve this problem. – camickr Mar 08 '13 at 04:07
3

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);
camickr
  • 321,443
  • 19
  • 166
  • 288