How to do drag and drop getting images from web browser in Java Swing?

I'm trying to get images from a web browser using drag and drop, but it fails:

package tests;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;

public class JDragDropTest extends JFrame {

    public JDragDropTest() throws HeadlessException {

        super("DragDropTest");

        setLayout(new BorderLayout());

        add(new JScrollPane(new JTextArea() {{
            setTransferHandler(new TransferHandler() {
                @Override
                public boolean canImport(TransferSupport support) {
                    return true;
                }

                @Override
                public boolean importData(TransferSupport support) {
                    int i=0;
                    append("flavors:\n");
                    for( DataFlavor flavor : support.getDataFlavors() ) {
                        append(String.valueOf(i+1) + "): " + flavor.toString() + "\n");
                        i++;
                    }
                    append("");

                    return true;
                }
            });
        }}), BorderLayout.CENTER);

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                JFrame frame = new JDragDropTest();

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(800, 600);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });

    }
}

      

When dragging from Google Chrome, I get a list of 131 flavors with no image.

Some of them have a view class InputStream

, I read some of them and got type text IMG SRC=

.

When dragging from Internet Explorer, I get an empty list.

When I try to drag from Firefox, I can't even start dragging.

OS is window 8.1 prof.

I would like to get an image, not a URL, not an IMG tag. Is it possible?

+3


source to share


1 answer


Here's my attempt ( not tested ):

  • I test only Windows 7

    , JDK 1.8.0_51

    , FireFox 39.0

    , Chrome 45.0

    , stackoverflow profile Icon

    .


enter image description here

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.List;
import javax.swing.*;

public class JDragDropTest2 {
  public JComponent makeUI() {
    final JLabel label = new JLabel("Drop here image from a web browser");
    label.setTransferHandler(new TransferHandler() {
      @Override public boolean canImport(JComponent component, DataFlavor[] flavors) {
        return true;
      }
      @Override public boolean importData(JComponent component, Transferable transferable) {
        label.setText("");
        try {
          for (DataFlavor flavor : transferable.getTransferDataFlavors()) {
            System.out.println(flavor);
            if (DataFlavor.imageFlavor.equals(flavor)) {
              Object o = transferable.getTransferData(DataFlavor.imageFlavor);
              if (o instanceof Image) {
                label.setIcon(new ImageIcon((Image) o));
                return true;
              }
            }
            if (DataFlavor.javaFileListFlavor.equals(flavor)) {
              Object o = transferable.getTransferData(DataFlavor.javaFileListFlavor);
              if (o instanceof List) {
                List list = (List) o;
                for (Object f : list) {
                  if (f instanceof File) {
                    File file = (File) f;
                    System.out.println(file);
                    if (!file.getName().endsWith(".bmp")) {
                      label.setIcon(new ImageIcon(file.getAbsolutePath()));
                      return true;
                    }
                  }
                }
              }
            }
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
        return false;
      }
      @Override public int getSourceActions(JComponent component) {
        return COPY;
      }
    });
    return new JScrollPane(label);
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JDragDropTest2().makeUI());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

      

+1


source







All Articles