Here's a utility class I wrote

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;


/**
 Singleton that allows String or Transferables (Images) to be placed
in the
 clipboard. Call boolean result=ClipboardUtils.setContent(var)
if result is false, something went wrong

 */
public class ClipboardUtils {
        private final static Toolkit toolkit = Toolkit.getDefaultToolkit();
        private final static Clipboard clipboard =
toolkit.getSystemClipboard();

        private ClipboardUtils() {

        }
        public static boolean setContent(StringBuffer content) {
                return setContent(content.toString());
        }

        public static boolean setContent(String content) {
                boolean result = true;
                try {
                        StringSelection stringselection = new StringSelection(content);
                        clipboard.setContents(stringselection, stringselection);
                } catch (IllegalStateException e) {
                        result = false;
                }
                return result;
        }
        public static boolean setContents(Transferable contents) {
                return setContents(contents, null);
        }
        public static boolean setContents(
                Transferable contents,
                ClipboardOwner owner) {
                boolean result = true;
                try {
                        clipboard.setContents(contents, owner);
                } catch (IllegalStateException e) {
                        result = false;
                }
                return result;
        }
}

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to