Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #731

Re: Image to ClipBoard (linux)

From rossum <rossum48@coldmail.com>
Newsgroups comp.lang.java.help
Subject Re: Image to ClipBoard (linux)
Date 2011-06-03 00:28 +0100
Message-ID <ll6gu69d0cgtettlk465l4979kvcbhrv41@4ax.com> (permalink)
References <4de7e5e5$0$7305$426a34cc@news.free.fr> <252gu6d1j879vs2dv6brd74fms1nf496e3@4ax.com> <4de80f1f$0$32685$426a74cc@news.free.fr>

Show all headers | View raw


On 02 Jun 2011 22:30:55 GMT, ricoh51 <ricoh51@free.fr> wrote:

>On Thu, 02 Jun 2011 23:05:33 +0100, rossum wrote:
>
>> On 02 Jun 2011 19:35:01 GMT, ricoh51 <ricoh51@free.fr> wrote:
>> 
>>>	public void copyToClipboard(){
>>>		Clipboard clipboard = Toolkit.getDefaultToolkit
>>>().getSystemClipboard();
>>>		ImageTransferable selection = new ImageTransferable
>>>(image);
>>>		clipboard.setContents(selection, null);
>> I am not sure if it is causing your problem, but in my clipboard code I
>> set the owner parameter here to 'this', not to 'null'.
>
>but setContents needs a :
>java.awt.datatransfer.Transferable,java.awt.datatransfer.ClipboardOwner
>and "this" is not a ClipboardOwner in my example.
>Do you have a minimal example?
>
>Best Regards
>
>eric
What I have is a very simple utility class that I use to protect
myself from the full horror of the Java clipboard.  It is text only
though, not images.  If you get yours working I will probably add
that.

Careful with the line wrap, this is as written and not reformatted for
usenet.

rossum


// *** Code starts ***

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;


/**
 * a simple interface with the Windows clipboard.
 *
 * @author Martin Ross
 */
public class MyClipBoard implements ClipboardOwner {

    public MyClipBoard() {}

    /** 
     * no action taken or needed. Required as part of the
ClipboardOwner Interface.
     */
    public void lostOwnership(Clipboard clipboard, Transferable
contents) {
        // No action required.
    }

    /**
     * adds some text to the clipboard.
     *
     * @param text the text to copy to the clipboard.
     */
    public void setText(String text) {
        StringSelection ssText = new StringSelection(text);
        Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(ssText, this);
    } // end setText()

    /**
     * clears the clipboard.
     */
    public void clear() {
        Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(null, this);
    } // end clear()


    /**
     * retrieves text from the clipboard.
     *
     * @return the text retrieved from the clipboard or the empty
string.
     *
     * @throws java.lang.IOException if the clipboard holds the wrong
type of data.
     * @throws java.lang.IOException if there was an error accessing
the clipboard.
     */
    public String getText() throws IOException {
        String result = "";
        Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText = (contents != null) &&
            contents.isDataFlavorSupported(DataFlavor.stringFlavor);
        if ( hasTransferableText ) {
            try {
                result = (String)
contents.getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException ufe) {
                throw new IOException("MyClipBoard.getText: text data
not supported.", ufe);
            } catch (IOException ioe) {
                throw new IOException("MyClipBoard.getText: clipboard
unavailable.", ioe);
            } // end try/catch
        } // end if
        return result;
    } // end getText()

} // end class MyClipBoard

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Image to ClipBoard (linux) ricoh51 <ricoh51@free.fr> - 2011-06-02 19:35 +0000
  Re: Image to ClipBoard (linux) rossum <rossum48@coldmail.com> - 2011-06-02 23:05 +0100
    Re: Image to ClipBoard (linux) ricoh51 <ricoh51@free.fr> - 2011-06-02 22:30 +0000
      Re: Image to ClipBoard (linux) rossum <rossum48@coldmail.com> - 2011-06-03 00:28 +0100
  Re: Image to ClipBoard (linux) "John B. Matthews" <nospam@nospam.invalid> - 2011-06-03 00:19 -0400
    Re: Image to ClipBoard (linux) ricoh51 <ricoh51@free.fr> - 2011-06-03 09:15 +0000
      Re: Image to ClipBoard (linux) "John B. Matthews" <nospam@nospam.invalid> - 2011-06-03 15:01 -0400

csiph-web