Solved

clipboard question

Posted on 2002-07-11
1
191 Views
Last Modified: 2012-06-27
Im trying to make a simple program that can tell whenever something new is copied to the clipboard.  This is what I have:

class ClipboardWatcher {

   public static void main (String[] args) {
      init();
   }

   public static void init() {
      String s = getClipboard();
      ClipboardOwner owner = new MyClipboardOwner();
      setClipboard(s, owner);
   }

   public static String getClipboard() {
      Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
      String s = " ";
      try {
         if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            s = (String)t.getTransferData(DataFlavor.stringFlavor);
          }
      } catch (UnsupportedFlavorException e) {
      } catch (java.io.IOException e) {
      }
      return s;
   }

   public static void setClipboard (String s, ClipboardOwner owner) {
      StringSelection ss = new StringSelection(s);
      Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, owner);
   }
}

class MyClipboardOwner implements ClipboardOwner {
     
   public void lostOwnership(Clipboard clipboard, Transferable contents) {
      System.out.println("Clipboard has changed");
      ClipboardWatcher.init();
      System.out.println(MainFrame.getClipboard());
   }
}

The way the program is supposed to work, it is should print out "Clipboard has changed" and then print the new clipboard contents.  The problem is that the program enters in an infinite loop with this code as soon as the user copys something.  What I've found is that the getClipboard() method does not get the new clipboard text, instead it returns the text that the clipboard had when the program was first run.

What am I doing wrong here?

Thanks for your time.
0
Comment
Question by:lwinkenb
1 Comment
 
LVL 9

Accepted Solution

by:
Ovi earned 100 total points
Comment Utility
I believe the method lostOwnership get's called before the content of the clipboard was released and replaced with the new one.

Try this :
   public void lostOwnership(Clipboard clipboard, Transferable contents) {
     new Thread(new Runnable() {
       public void run() {
         try {Thread.sleep(500);} catch(Exception e) {}
         System.out.println("Clipboard has changed");
         ClipboardWatcher.init();
         System.out.println(MainFrame.getClipboard());
       }
     }).start();
   }
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now