1

I have the next JTable

private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {"Hat", "New York", "1000"},
            {"T-Shirt", "New York", "3500"},
            {"Sweater", "Washington", "2800"},
            {"Bag", "California", "7000"},
        },
        new String [] {
            "Name", "Warehouse", "Quantity"
        }
    ));
    jScrollPane1.setViewportView(jTable1);

    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(19, 11, 375, 170);

    jButton1.setText("Copy");
    getContentPane().add(jButton1);
    jButton1.setBounds(113, 195, 63, 23);

    jButton2.setText("Exit");
    getContentPane().add(jButton2);
    jButton2.setBounds(225, 195, 53, 23);

    pack();
}

I want the button to copy the information of the model to the clipboard, but it is not working, i have tried using

String myString = "This text will be copied into clipboard when running this code!";
StringSelection stringSelection = new StringSelection (myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
clpbrd.setContents (stringSelection, null);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Washu
  • 835
  • 1
  • 9
  • 20

2 Answers2

8

The concept is relatively simple, the implementation a little more complicated.

The trick is to find mime types that the various applications will recognize and allow you to translate your data.

I've not included the CSV translation, it wouldn't take much to get that to work from the example code, also, I've not included any "formatting" of the table cell values, I'm sure you can figure that out as well.

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class CopyTableModel {

    public static void main(String[] args) {
        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"});

        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        cb.setContents(new TableTransferable(model), new ClipboardOwner() {
            @Override
            public void lostOwnership(Clipboard clipboard, Transferable contents) {
                System.out.println("You lose :(");
            }
        });

    }

    public static class TableTransferable implements Transferable {

        public static final DataFlavor TABLE_DATA_FLAVOR = new DataFlavor(TableModel.class, "binary/x-java-tablemodel; class=<javax.swing.TableModel>");
        public static final DataFlavor HTML_DATA_FLAVOR = new DataFlavor("text/html", "HTML");
        public static final DataFlavor CSV_DATA_FLAVOR = new DataFlavor("text/csv", "CVS");
        public static final DataFlavor PLAIN_DATA_FLAVOR = new DataFlavor("text/plain", "Plain text");
        public static final DataFlavor SERIALIZED_DATA_FLAVOR = new DataFlavor(String.class, "application/x-java-serialized-object; Plain text");
        private final TableModel model;

        public TableTransferable(TableModel model) {
            this.model = model;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{TABLE_DATA_FLAVOR, HTML_DATA_FLAVOR, CSV_DATA_FLAVOR, SERIALIZED_DATA_FLAVOR};
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            System.out.println("isSupported " + flavor);
            boolean supported = false;
            for (DataFlavor mine : getTransferDataFlavors()) {
                if (mine.equals(flavor)) {
                    supported = true;
                    break;
                }
            }
            return supported;
        }

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            System.out.println("get " + flavor);
            Object data = null;
            if (TABLE_DATA_FLAVOR.equals(flavor)) {
                data = model;
            } else if (HTML_DATA_FLAVOR.equals(flavor)) {
                data = new ByteArrayInputStream(formatAsHTML().getBytes());
            } else if (SERIALIZED_DATA_FLAVOR.equals(flavor)) {
                data = formatAsHTML();
            } else if (CSV_DATA_FLAVOR.equals(flavor)) {
                data = new ByteArrayInputStream("CSV".getBytes());
            } else {
                throw new UnsupportedFlavorException(flavor);
            }
            return data;
        }

        public String formatAsHTML() {
            StringBuilder sb = new StringBuilder(128);
            sb.append("<html><body>");
            sb.append("<table>");
            sb.append("<tr>");
            for (int index = 0; index < model.getColumnCount(); index++) {
                sb.append("<th>").append(model.getColumnName(index)).append("</th>");
            }
            sb.append("</tr>");
            for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
                sb.append("<tr>");
                for (int colIndex = 0; colIndex < model.getColumnCount(); colIndex++) {
                    Object o = model.getValueAt(rowIndex, colIndex);
                    // You will want to format the value...
                    String value = o == null ? "" : o.toString();
                    sb.append("<td>").append(value).append("</td>");
                }
                sb.append("</tr>");
            }
            sb.append("</table>");

            return sb.toString();
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

The default Copy function of JTable will copy selected rows of data to the clipboard.

See Action Map Action for an approach that gives you access to this Action. 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