1

I'ved searched some time but no satisfying result showed thus would like to ask here.

Basicallly I have a JAVA Swing GUI table, it has some rows, let's say 3 columns each row, you see "1 david good" on a row. the content of this row is converted from a raw message, i.e. a xml message.

What I want to do is, when I selected a row and press ctrl+c, the a raw msg could be copied to clipboard then I could paste it in other Windows application like notepad or Word.

I tried toolkit to get system clipboard and put raw message in(via keylistener). But when I press ctrl+v in notepad, I still get "1 david good" as default.

I printed the raw message from clipboard in Swing code, thus I am guessing if my code only works in Swing, customized content could not be retrieved in Windows?

Could somebody tell me if it's possible to do in Swing? thanks a lot.

David ten Hove
  • 2,748
  • 18
  • 33
user462872
  • 323
  • 1
  • 4
  • 14
  • 1
    The `JTable` creates its own `Transferable` when copy-pasting. You need to adjust the created `Transferable`. An example can be found [here](http://stackoverflow.com/a/8776974/1076463). That example works the other way: the contents in the model is different from the rendered strings, and the rendered strings are copy-pasted instead of the model contents. – Robin Jan 27 '15 at 16:39

1 Answers1

2

You can use this example:

String str = "Which String to copy to clipboard";
StringSelection stringSelection = new StringSelection (str);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard ();
clpbrd.setContents (stringSelection, null);

So this code can be when clicking on some button while calling it from actionPerformed()

And for pasting issue:

  public String getClipboardContents() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    //odd: the Object param of getContents is not currently used
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText =
      (contents != null) &&
      contents.isDataFlavorSupported(DataFlavor.stringFlavor)
    ;
    if (hasTransferableText) {
      try {
        result = (String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      catch (UnsupportedFlavorException | IOException ex){
        System.out.println(ex);
        ex.printStackTrace();
      }
    }
    return result;
  }
roeygol
  • 4,908
  • 9
  • 51
  • 88
  • Hi Roey. I think the code only works inside Swing(what you paste in Windows is not you see in system.out) – user462872 Jan 28 '15 at 08:50
  • I can't understand what you're saying. This code must work using Swing. – roeygol Jan 28 '15 at 10:25
  • I meant, if I did nothing, Windows will copy "1 david good" (exactly what i see) from JTable and I could paste it anywhere, like a Notepad, or Word, even chrome. In the Swing I could add codes to the 'ctrl+c' event, to let the java actually copy another string like "a=1,b=david, c=good" into the system clipboard. but when I press 'ctrl+v' in Notepad or any other Windows applications, what I get is still "1 david good". – user462872 Jan 29 '15 at 06:02