Solved

clipboard question

Posted on 2002-07-11
1
197 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 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

Technology Partners: 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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

740 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