0

I'm looking to drag and drop an image from my desktop onto my JPanel/Jframe then be able to store that image into a BufferedImage object and do what id like with it

is there a way to do this with MouseListener?

here's a made-up example of a perfect world in which I could do this

    Jframe jframe = new Jframe();
    //set up jframe bla bla bla
    
    MouseListener ml = new MouseListener(){
       mouseRealeased(MouseEvent e){
            //get droped thing from e
            if(check if dropped thing is an image){
                this.myImageField = (BufferedImage)droppedThing;
            }
       }
    }        
    jframe.addMouseListener(ml); 

I hope you get the idea. not looking for something super complicated. I'm a noob keep it as simple as possible thanks :)

TBA
  • 1,921
  • 4
  • 13
  • 26
jacobG
  • 3
  • 2
  • For [example](https://stackoverflow.com/questions/71503700/how-to-drop-images-into-a-jinternalframe/71504636#71504636); [example](https://stackoverflow.com/questions/13597233/how-to-drag-and-drop-files-from-a-directory-in-java/13597635#13597635); [example](https://stackoverflow.com/questions/15080244/java-drag-n-drop-files-of-specific-extension-on-jframe/15080654#15080654); [example](https://stackoverflow.com/questions/36571996/drag-two-images-and-drop-them-in-two-different-jbuttons-in-the-same-jframe/36572627#36572627) – MadProgrammer Jun 04 '22 at 03:41
  • I think this question has been answered, check this or – Kingisdave Jun 04 '22 at 03:42

1 Answers1

0
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.UIManager;

public class FileDrag {

    public static void main(String[] args) {
        new FileDrag();
    }

    public class FilePane extends JLayeredPane {

        public FilePane() {
            File[] images = new File("C:\\hold\\thumbnails").listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    String name = pathname.getName().toLowerCase();
                    return name.endsWith(".png") || 
                                    name.endsWith(".jpg") || 
                                    name.endsWith(".bmp") ||
                                    name.endsWith(".gif");
                }
            });

            int x = 0;
            int y = 0;
            for (File imgFile : images) {

                try {
                    BufferedImage img = ImageIO.read(imgFile);
                    JLabel label = new JLabel(new ImageIcon(img));
                    label.setSize(label.getPreferredSize());
                    label.setLocation(x, y);
                    MouseHandler mh  = new MouseHandler();
                    label.addMouseListener(mh);
                    label.addMouseMotionListener(mh);
                    add(label);
                    x += 20;
                    y += 20;
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 800);
        }

        public class MouseHandler extends MouseAdapter {

            private Point offset;

            @Override
            public void mousePressed(MouseEvent e) {
                JLabel label = (JLabel) e.getComponent();
                moveToFront(label);
                offset = e.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                int x = e.getPoint().x - offset.x;
                int y = e.getPoint().y - offset.y;
                Component component = e.getComponent();
                Point location = component.getLocation();
                location.x += x;
                location.y += y;
                component.setLocation(location);
            }
        }
    }
}

You can also refer to How can I drag images with the mouse cursor in Java GUI?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Doesn’t this just support dragging and dropping images from one jpanel to itself or another tho? I’m looking to drag one from my desktop to my Jpannel – jacobG Jun 04 '22 at 04:06
  • That's just like the simple version you asked for, but you can make it work for the entirety of your application with some more tweaks – Kingisdave Jun 04 '22 at 04:30
  • is this working or something? I curious if trying to implement the drag & drop from text into it. – gumuruh Mar 14 '23 at 08:16