Solved

clipboard question

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 71
Java array sort 10 63
Java regex 5 22
Selenium WebElement Login button findElement 9 22
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…
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 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