Drag N Drop Icons

I would like the users to hold the mouse on a component such as a label with an icon. Then i would like him to be able to drag the actual label around on the screen and register where he drops it and what label it is?
A tutorial would be best if posible. It seems all the tuts here are about draging and dropping text, what about icons/ components.
Thanks

I suppose you saw this one already, then?
http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html
Or here's another set; dragging and dropping in JTree sounds like it's not text-oriented:
http://www.javaolympus.com/J2SE/JFC/DragDrop/DragDrop.jsp
These are just the first ones I found when I searched Google for the keywords "java drag and drop tutorial".

Similar Messages

  • Drag and drop icons from onside of the split pane to the other side.

    Hello All,
    I am tring to write a program where i can drag and drop icons from the left side of the split pane to the right side. I have to draw a network on the right side of the split pane from the icons provided on the left side. Putting the icons on the labels donot work as i would like to drag multiple icons from the left side and drop them on the right side. CAn anyone please help me with this.
    Thanks in advance
    smitha

    The other option besides the drag_n_drop support
    http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.htmlis to make up something on your own. You could use a glasspane (see JRootPane api for overview) as mentioned in the tutorial.
    Here's another variation using an OverlayLayout. One advantage of this approach is that you can localize the overlay component and avoid some work.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    public class DragRx extends JComponent {
        JPanel rightComponent;
        BufferedImage image;
        Point loc = new Point();
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(image != null)
                g2.drawImage(image, loc.x, loc.y, this);
        public void setImage(BufferedImage image) {
            this.image = image;
            repaint();
        public void moveImage(int x, int y) {
            loc.setLocation(x, y);
            repaint();
        public void dropImage(Point p) {
            int w = image.getWidth();
            int h = image.getHeight();
            p = SwingUtilities.convertPoint(this, p, rightComponent);
            JLabel label = new JLabel(new ImageIcon(image));
            rightComponent.add(label);
            label.setBounds(p.x, p.y, w, h);
            setImage(null);
        private JPanel getContent(BufferedImage[] images) {
            JSplitPane splitPane = new JSplitPane();
            splitPane.setLeftComponent(getLeftComponent(images));
            splitPane.setRightComponent(getRightComponent());
            splitPane.setResizeWeight(0.5);
            splitPane.setDividerLocation(225);
            JPanel panel = new JPanel();
            OverlayLayout overlay = new OverlayLayout(panel);
            panel.setLayout(overlay);
            panel.add(this);
            panel.add(splitPane);
            return panel;
        private JPanel getLeftComponent(BufferedImage[] images) {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            for(int j = 0; j < images.length; j++) {
                gbc.gridwidth = (j%2 == 0) ? GridBagConstraints.RELATIVE
                                           : GridBagConstraints.REMAINDER;
                panel.add(new JLabel(new ImageIcon(images[j])), gbc);
            CopyDragHandler handler = new CopyDragHandler(panel, this);
            panel.addMouseListener(handler);
            panel.addMouseMotionListener(handler);
            return panel;
        private JPanel getRightComponent() {
            rightComponent = new JPanel(null);
            MouseInputAdapter mia = new MouseInputAdapter() {
                Component selectedComponent;
                Point offset = new Point();
                boolean dragging = false;
                public void mousePressed(MouseEvent e) {
                    Point p = e.getPoint();
                    for(Component c : rightComponent.getComponents()) {
                        Rectangle r = c.getBounds();
                        if(r.contains(p)) {
                            selectedComponent = c;
                            offset.x = p.x - r.x;
                            offset.y = p.y - r.y;
                            dragging = true;
                            break;
                public void mouseReleased(MouseEvent e) {
                    dragging = false;
                public void mouseDragged(MouseEvent e) {
                    if(dragging) {
                        int x = e.getX() - offset.x;
                        int y = e.getY() - offset.y;
                        selectedComponent.setLocation(x,y);
            rightComponent.addMouseListener(mia);
            rightComponent.addMouseMotionListener(mia);
            return rightComponent;
        public static void main(String[] args) throws IOException {
            String[] ids = { "-c---", "--g--", "---h-", "----t" };
            BufferedImage[] images = new BufferedImage[ids.length];
            for(int j = 0; j < images.length; j++) {
                String path = "images/geek/geek" + ids[j] + ".gif";
                images[j] = ImageIO.read(new File(path));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new DragRx().getContent(images));
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
    class CopyDragHandler extends MouseInputAdapter {
        JComponent source;
        DragRx target;
        JLabel selectedLabel;
        Point start;
        Point offset = new Point();
        boolean dragging = false;
        final int MIN_DIST = 5;
        public CopyDragHandler(JComponent c, DragRx dr) {
            source = c;
            target = dr;
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            Component[] c = source.getComponents();
            for(int j = 0; j < c.length; j++) {
                Rectangle r = c[j].getBounds();
                if(r.contains(p) && c[j] instanceof JLabel) {
                    offset.x = p.x - r.x;
                    offset.y = p.y - r.y;
                    start = p;
                    selectedLabel = (JLabel)c[j];
                    break;
        public void mouseReleased(MouseEvent e) {
            if(dragging && selectedLabel != null) {
                int x = e.getX() - offset.x;
                int y = e.getY() - offset.y;
                target.dropImage(new Point(x,y));
            selectedLabel = null;
            dragging = false;
        public void mouseDragged(MouseEvent e) {
            Point p = e.getPoint();
            if(!dragging && selectedLabel != null
                         && p.distance(start) > MIN_DIST) {
                dragging = true;
                copyAndSend();
            if(dragging) {
                int x = p.x - offset.x;
                int y = p.y - offset.y;
                target.moveImage(x, y);
        private void copyAndSend() {
            ImageIcon icon = (ImageIcon)selectedLabel.getIcon();
            BufferedImage image = copy((BufferedImage)icon.getImage());
            target.setImage(image);
        private BufferedImage copy(BufferedImage src) {
            int w = src.getWidth();
            int h = src.getHeight();
            BufferedImage dst =
                source.getGraphicsConfiguration().createCompatibleImage(w,h);
            Graphics2D g2 = dst.createGraphics();
            g2.drawImage(src,0,0,source);
            g2.dispose();
            return dst;
    }geek images from
    http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html

  • Desktop drag and drop icon for my "other" computer

    i really need an icon for my MacBookPro desktop to reside on my Mac Pro desktop (and vice versa) so that I can just drag and drop copy (or move) data from one volume to another. sort of like what I have with my dropbox icon in the sense that I can just keep this on my desktop and in the View Pane of Finder and drag and drop items or open the volume etc.
    is this possible and i am just not getting it to work because i don't have things set to mount automatically or is there a restriction that does not allow this that I don't understand?
    i am on snow leopard and the connection is via wi, but I could plug in via ethernet when i know i will need to do this sort of moving of data a lot.
    thanks for clearing any of this up for me.
    - jon

    if as3, create a sprite or movieclip and use:
    image1.addEventListener(MouseEvent.MOUSE_DOWN,downF);
    image1.addEventListener(MouseEvent.MOUSE_UP,upF);
    function downF(e:MouseEvent):void{
    Sprite(e.currentTarget).startDrag();
    function upF(e:MouseEvent):void{
    Sprite(e.currentTarget).stopDrag();

  • Drag and drop icons

    hello world....
    in the process of learning i'm trying to create a small java app that resizes and processes graphics, (using imagemagick api).
    what i'd like to be able to do is drag the icon (or shortcut) to my java app and then have the app process that, just as a windows app would do. in fact all i need really is the location of the file the user has dragged and dropped onto my app, although it would be nice if i could indicate somewhere that the file was being processed!
    i understand the use of the drag and drop re the text within java, but how do i go about dragging an external icon over a java app and having that pass me the file location (which is actually all i need...)
    thanks in advance!

    i understand the use of the drag and drop re the text
    within java, Are you sure?
    Read this:
    http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html

  • Drag and drop icons between regions

    I have two regions in a page. Each of the regions show employers available in different departments and each employer is represented with icons. is it possible to drag and drop employees between these two regions (departments) and finally save the latest changes made to the employes/deparments into the table?. any ideas are appreciated.
    thanks,
    Surya

    Yes it is possible, but not so easy. If you'll be at OOW this year, you can come to my session (S301752, Monday 13:00-14:00) - I will show something similar there. If you're not so lucky, you can read the excellent blogpost of my colleague at [http://rutgerderuiter.blogspot.com/2008/07/drag-planboard.html]
    Cheers
    Roel

  • I am unable drag and drop icons on to customized toolbar... they simply will not drag How do I resolve this? Do I need to download something?

    I click to customize toolbar and add a new one. Then I try to drag an icon from the panel onto the new toolbar. Nothing. I am unable to do so. None of the icons are grabbable, despite the fact that the fat hand appears when I hover over them, indicating that they are ready for drag and drop (or at least that is what I am programmed to think)

    See:
    * http://kb.mozillazine.org/Corrupt_localstore.rdf
    Start Firefox in [[Safe Mode]] to check if one of the add-ons is causing the problem (switch to the DEFAULT theme: Tools > Add-ons > Appearance/Themes).
    * Don't make any changes on the Safe mode start window.
    See:
    * [[Troubleshooting extensions and themes]]

  • Cannot drag and drop icons,

    I'm working on a Mac Power PC G4, OS X, version 10.2.8. About a week ago I started noticing a problem dragging and dropping items with the trackpad. I can click on a file to open it, but I cannot drag and drop files to move them. If I am in a Word document, I can no longer drag highlighted text around either, or drag songs to playlists in iTunes. However, if I open a window, I can drag the open window around, just not any of the files displayed in it. The same thing happens when I hook up a mouse. I've checked the Finder view options and have done a test to check that it is not a corrupted system preferences and have tried many of the things listed on discussions I have seen online. I'm hoping that I don't have to resort to reinstalling the operating system. Everything was working fine until about a week ago, and I am aware of no changes or damage to the computer that could have caused this problem. Any help?

    I created a new account with administrative privelages, but the new account still does the same thing. I also tried trashing com.apple.systemuiserver.plist as suggested to me by someone else, and it had no effect.
    Thanks for you idea.

  • Can no longer drag and drop icons

    Hello All
    All of a sudden no users on my mac can "drag and drop" on desktop in finder or in the dock. can move open windows. have tried my old mouse and just the same
    also firefox stopped working at the same time wont load!
    Help please

    Hi again,
    I am sorry that didn't work. The only other thing i can think of is, have you checked the setting in each user account? In system preferences, users or user accounts (sorry i am on a PC at the moment so can't remember what it is called)
    I know there is a setting in there to stop users making changes to the dock, maybe this has changed and that is causing these problems?

  • How to drag and drop icon in the browser

    hi
    i Have A web page that i should drag some responces from right to their correct location in Left.in pc I hold left mouse buttom and drag icon into its Correct place and drop it but I can't do that on the android browser. how should I do that? and firefox is only
    browser that support my web page.

    Some websites are programed to recognize tap actions instead of clicks, but others design the site to have a mobile version and a desktop version.
    However I do not know if this will do this automatically. For example if you have a drop down menu in a form, it will drop down if you tap it on a mobile device and will do the same thing if you click it in a desktop browser.
    It is possible to try to long tap then try to drag it, but if the website does not have it programmed, it may not work. There is Javascript UI touch punch that you can check out to develop this capability, but that would be a question for stackoverflow.com.
    I am sorry I could not be more helpful.

  • Will we ever get to drag and drop icons into our schedules, ever?

    I was wondering if iCal would ever have icons like most calenders, ones we can copy and paste from out clipboards to use for meeting schedules, dates, anything we like.
    will it ever happen YET?

    they been around since the 80's, anyone want these feature?
    atleast to see the contacts fotos in the calender to have a hint in a months view, as a pop up, something?

  • I can't drag and drop icons

    Whenever I click on something and try and drag it somewhere else it just doesn't work at all.

    Restart and see if that solves your issue.

  • I cannot drag and drop

    I was trying to clean up my computer yesterday by deleting some files, mainly clearing the cache and deleting large files that i knew what they were.  I turned off my computer for the night, and now today when i turned on my computer I can no longer drag and drop icons or cut and paste files from folder to folder. 

    Also, after the video thumbnail is ghosted, if I click on the ghosted thumbnail the thumbnail image does not appear in the preview window. Instead, the preview window turns black.
    Also, if I double click the ghosted thumbnail I do not get the Clip info dialog. It's as if the clip no longer exists, but I can see it's ghosted thumbnail.
    Also, I showed my wife the problem and she just told me she was using iMovie 2 days ago with no problem.
    Ralph Ocampo

  • Can not drag and drop or copy and paste

    It is as simmple as the title. I can not drag and drop icons or text or folders. Nothing. As for cut, copy, and paste they do not work either. I am trying to back up my files so I can do a clean install of snow leopard. I have leopard currently.

    So Copy/Paste, etc. isn't working in say Text Edit?
    On the Drag & Drop, where are you trying to drop them, & does the icon move but just not copy?

  • Drag and drop help (urgent)

    I need to write a program where I can drag and drop icons between and within a split pane. Any suggestion of what package I should look into? thanks

    Java provides two separate packages for drag and drop. java.awt.dnd and java.awt.datatransfer. These packages contain interfaces and classes for drag and drop. So pls go thru these classes.

  • I can't drag and drop anylonger!!!

    Apple family! I can't drag and drop icons anylonger... therefore, I can't copy stuff from one folder to another!!! REALLY BAD! What can I do? Thanks.

    From:
    https://discussions.apple.com/message/18436580#18436580
    Press Command(cmd) + Alt+ Esc, select Finder and press "relaunch". That fixed it for me

Maybe you are looking for

  • JVM's scheduling policy

    Greetings, I would like to know what is the JVM's scheduling policy? what does it aims for (i.e., low waiting time, low response time, low turnaround time)? If any of you have an idea, please share it with me... Kind regards and thanks in advanced, O

  • Loss of Synchronization for Finite Pulse Train generation

    I have successfully generated a finite pulse train on my 6608. My program is based off an example I got from NI Zone (Square_Wave_Trigger.zip). Unfortunately, the Finite Pulse Train loses synchronization every once in a while, and I'm not sure why. I

  • Kona 3 Down Conversion Workflow

    I have just set up an online system for a project shot on Digibeta and HDV. I am in the process of doing an edit on my offline suite which involves 3 tracks shot on Digibeta and 1 track on HDV 1080i. I captured the digi's in DVPAL and downconverted t

  • Problem to connect Developer Suite forms 9 with Oracle 9i Database

    Hi, I have a problem to connect Developer Suite Release 2 forms 9 with Oracle 9i database release 1. I have done net8 easy configuration but no success. Can any one help me to solve this problem. Thanks in Advance Nasir Ali Mughal

  • Quality Inspection stock and invoice

    Hi, I created a PO, GR and INV successfully. I received the stock to quality inspection. After created invoice i did MB1B stock transfer(321). My question is when you received the stock into quality inspection how can we post an invoice for payment b