Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

clipboard question

Posted on 2002-07-11
1
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
Ovi earned 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

705 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