15

The thing I would want to do is that when the user copies a text in any program (firefox, notepad, pdfReader etc.) my already running java application shall be informed and immediately shows a popup.

I think in order to be able to do this my java application should be invoked whenever system clipboard is changes.

Is that possible with java, if so in which version? I know we can reach and manipulate system clipboard content but my specific question is about invoking the java app. when clipboard content changes.

Thanks

dds
  • 548
  • 1
  • 6
  • 16

1 Answers1

8

I would have naively pretended it's a job for JDIC, but the interwebz told me the truth. So, let me explain a little.

using Toolkit.getSystemClipboard(), you can get access to the native system clipboard. Like any Java object, this clipboard can be listened. Precisely, you can call Clipboard.addFlavorListener(...) to listen to FlavorEvents. What are they ? They're the metal kings ! .... no no no, I completely and utterly digress. Let me come back. So, a FlavorEvent, according to the doc, indicates that

that available DataFlavors have changed in the Clipboard (the event source).

Which may means that Clipboard content has changed. However, I would not go directly on it without a first prototype.

EDIT Example of a prototype from the fingers of AlexR

import java.awt.Toolkit;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;

public class Main {
    public static void main(String[] args) throws Exception { 
        Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(new FlavorListener() { 
            @Override 
            public void flavorsChanged(FlavorEvent e) { 
                System.out.println("changed!!! " + e.getSource() + " " + e.toString()); 
            } 
        }); 
        Thread.sleep(100000L); 
    }
}
Community
  • 1
  • 1
Riduidel
  • 22,052
  • 14
  • 85
  • 185
  • 1
    interesting, but does this fire if the contents change without the DataFlavors changing, I wonder? – bdonlan Jan 26 '11 at 09:48
  • 1
    That's why I say a prototype has to be made :-) It is not a proven solution, only a hint at the simplest possible solution. – Riduidel Jan 26 '11 at 09:49
  • 2
    I have just tried this and this really works! Thanks, @Riduidel! Here is my code: public static void main(String[] args) throws Exception { Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(new FlavorListener() { @Override public void flavorsChanged(FlavorEvent e) { System.out.println("changed!!! " + e.getSource() + " " + e.toString()); } }); Thread.sleep(100000L); } – AlexR Jan 26 '11 at 09:53
  • @dds I expected it so :-) Thanks to @AlexR for its brilliant example. – Riduidel Jan 26 '11 at 13:31
  • 5
    This approach works only when copying from an application once at a time. As Flavor event will not change if copy one more time at the same application. Monitoring by clipboard owner change would be more reliable. Here is the details: http://stackoverflow.com/questions/5484927/listen-to-clipboard-changes-check-ownership – Yu Shen Apr 12 '12 at 22:19
  • 1
    This does not work if new data is copied that has the same flavor. – Boann Dec 16 '13 at 12:58
  • I do not understand why this is accepted or received any upvotes, as it does not fire when the flavor did not change. – scravy Sep 22 '17 at 11:19
  • As @scravy already mentioned, it does not answer the question. – It's Leto Apr 19 '18 at 08:11