Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6743183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:55:51+00:00 2026-05-26T11:55:51+00:00

I’m making a program using java that sends the clipboard contents over sockets; I

  • 0

I’m making a program using java that sends the clipboard contents over sockets; I managed to make it work with strings but I’m having some troubles with images. Here is the code:

    //get Image
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Image imageContents = (Image)clipboard.getData(DataFlavor.imageFlavor);
    ImageIcon image = new ImageIcon(imageContents);

    //sent over sockets

    //set Image
    String mime = DataFlavor.imageFlavor.getMimeType();
    DataHandler contents = new DataHandler(image,mime);

    //set clipboard
    clipboard.setContents(contents, null);

After setContents the clipboard is empty; Any ideas why, and how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:55:52+00:00Added an answer on May 26, 2026 at 11:55 am

    Here is some code I’ve used to write/read an Image from the clipboard. Never tried it with sockets so I’m not sure it will help:

    import java.awt.*;
    import java.awt.datatransfer.*;
    
    public class ClipboardImage
    {
        /**
         *  Retrieve an image from the system clipboard.
         *
         *  @return the image from the clipboard or null if no image is found
         */
        public static Image read()
        {
            Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents( null );
    
            try
            {
                if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor))
                {
                    Image image = (Image)t.getTransferData(DataFlavor.imageFlavor);
                    return image;
                }
            }
            catch (Exception e) {}
    
            return null;
        }
    
        /**
         *  Place an image on the system clipboard.
         *
         *  @param  image - the image to be added to the system clipboard
         */
        public static void write(Image image)
        {
            if (image == null)
                throw new IllegalArgumentException ("Image can't be null");
    
            ImageTransferable transferable = new ImageTransferable( image );
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
        }
    
        static class ImageTransferable implements Transferable
        {
            private Image image;
    
            public ImageTransferable (Image image)
            {
                this.image = image;
            }
    
            public Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException
            {
                if (isDataFlavorSupported(flavor))
                {
                    return image;
                }
                else
                {
                    throw new UnsupportedFlavorException(flavor);
                }
            }
    
            public boolean isDataFlavorSupported (DataFlavor flavor)
            {
                return flavor == DataFlavor.imageFlavor;
            }
    
            public DataFlavor[] getTransferDataFlavors ()
            {
                return new DataFlavor[] { DataFlavor.imageFlavor };
            }
        }
    
        public static void main(String[] args)
        {
            Image image = Toolkit.getDefaultToolkit ().createImage("???.jpg");
            ClipboardImage.write( image );
    
            javax.swing.ImageIcon icon = new javax.swing.ImageIcon( ClipboardImage.read() );
            javax.swing.JLabel label = new javax.swing.JLabel( icon );
    
            javax.swing.JFrame frame = new javax.swing.JFrame();
            frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
            frame.getContentPane().add( label );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.