Using the native getClipboard method in eclipse

Asked by Boipelo Mawasha

I'm making use of the following class for accessing information that's on the clipboard because the sikuli method (env.getClipboard) only works once in a while. However, I also get the error that's shown below when I use the java class. Any assistance on how to overcome this will be much appreciated:

Using: Sikuli-r830

Class:
public static String getClipboard() {
      Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
      try {
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
          String text = (String) t.getTransferData(DataFlavor.stringFlavor);

          return text;
        }
      } catch (Exception e) {
      }
      return "";
    }

Error:

Exception in thread "main" java.lang.IllegalStateException: cannot open system clipboard
 at sun.awt.windows.WClipboard.openClipboard(Native Method)
 at sun.awt.datatransfer.ClipboardTransferable.<init>(Unknown Source)
 at sun.awt.datatransfer.SunClipboard.getContents(Unknown Source)

The error only occurs if I try to run another iteration right after the first one is complete

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Boipelo Mawasha (bmawasha) said :
#1

I'm doing all of this in eclipse

Revision history for this message
RaiMan (raimund-hocke) said :
#2

I just tested the following:

Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); // only once

and then some iterations:

System.out.println(cb.getContents(null));

works fine.

So I guess, you can only get the clipboard once per app, but access it many times using this singled object.

Revision history for this message
Boipelo Mawasha (bmawasha) said :
#3

That works just fine. The problem is that I have to use the getSystemClipboard method constantly while I'm writing my code. Does this mean that I will have to close Eclipse everytime before I try and reuse the method?

Revision history for this message
RaiMan (raimund-hocke) said :
#4

no, this means, that you have to acquire the system clipboard only once and then use it afterwards all over the place.

example (might not be error free, only to demonstrate):

Class MyClipboard:

protected Clipboard _cb = null

static {
   _cb = Toolkit.getDefaultToolkit().getSystemClipboard()
}

public static String getContent() {
      Transferable t = _cb.getContents(null);
      try {
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
          String text = (String) t.getTransferData(DataFlavor.stringFlavor);

          return text;
        }
      } catch (Exception e) {
      }
      return "";
    }
}

in your code:

MyClipboard cb = new MyClipboard(); # acquires the system clipboard once

System.out.println(cb.getContent()) # use cb as often you like

Revision history for this message
RaiMan (raimund-hocke) said :
#5

no, this means, that you have to acquire the system clipboard only once and then use it afterwards all over the place.

example (might not be error free, only to demonstrate):

Class MyClipboard:

protected Clipboard _cb = null

static {
   _cb = Toolkit.getDefaultToolkit().getSystemClipboard()
}

public static String getContent() {
      Transferable t = _cb.getContents(null);
      try {
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
          String text = (String) t.getTransferData(DataFlavor.stringFlavor);

          return text;
        }
      } catch (Exception e) {
      }
      return "";
    }
}

in your code:

MyClipboard cb = new MyClipboard(); # acquires the system clipboard once

System.out.println(cb.getContent()) # use cb as often you like

Can you help with this problem?

Provide an answer of your own, or ask Boipelo Mawasha for more information if necessary.

To post a message you must log in.