Solved

clipboard question

Posted on 2002-07-11
1
193 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
ID: 7148350
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

863 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

20 Experts available now in Live!

Get 1:1 Help Now