Results 1 to 2 of 2
  1. #1
    estro is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Clipboard handling on Mac OS X

    Hi,

    I'm having troubles getting data from the system clipboard on Mac OS X.
    What I'm trying to do is to listen to the system clipboard and print the content of the clipboard each time new [text based] information is put into it.

    The problem: bellow code works perfectly fine on Windows 7 and openSuse Linux machines, however when I try running the same code on Mac OS X the program fails to print the new content of the clipboard until focus is given to the application. [Nothing is printed until I click on the application icon on the dock...]

    My source code:

    Java Code:
    import java.awt.Toolkit;  
    import java.awt.datatransfer.*;  
    import java.io.IOException;  
      
    public class ClipboardListener extends Thread implements ClipboardOwner {
    	
        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();  
          
        public void run(){  
            Transferable selection = systemClipboard.getContents(this);  
            gainOwnership(selection);  
            while (true) {}
        }  
          
        public void gainOwnership(Transferable t){ 
        	try {this.sleep(100);} 
        	catch (InterruptedException e) {}
            systemClipboard.setContents(t, this);  
        }  
          
        public void lostOwnership(Clipboard clipboard, Transferable contents) {
            try {System.out.println((String) clipboard.getData(DataFlavor.stringFlavor));} 
            catch (UnsupportedFlavorException e) {} 
            catch (IOException e) {}
            gainOwnership(contents);  
        }  
    }
    Java Code:
    public class myApp {
    
    	public static void main(String[] args){
    		ClipboardListener listener = new ClipboardListener();
    		listener.start();}
    }
    I will appreciate any help, thanks!
    Last edited by estro; 04-21-2012 at 11:05 AM.

  2. #2
    estro is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Clipboard on linux
    By banhbaochay in forum New To Java
    Replies: 1
    Last Post: 12-10-2010, 05:11 AM
  2. Clipboard and jTextPane
    By Gudradain in forum AWT / Swing
    Replies: 11
    Last Post: 01-05-2009, 12:08 AM
  3. Copy from Clipboard in japplet
    By AZMichael in forum Java Applets
    Replies: 3
    Last Post: 09-06-2008, 03:30 PM
  4. SWT Clipboard Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:34 PM
  5. Using the clipboard
    By Java Tip in forum java.awt.datatransfer
    Replies: 0
    Last Post: 04-16-2008, 11:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •