Search code examples
javajframejlabelimageicon

Making Jlabel(pictures) draggable around the Jframe?


How would I go upon making the labels(pictures) 'draggable'. So, basically to make it so I can click on the jlabel picture, and move it around the jframe and drop it. I've searched around but it was no help for me, so I was wondering if any of you know how to make it work?

package src;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
 * @Author - 0x29A
 * 
 * 
 */
public class Jframe {

    public static void main(final String args[]) {
        /*
         * @Images
         */
        final ImageIcon icon = new ImageIcon("Data/button.png");
        final JLabel label = new JLabel(icon);

        final ImageIcon icon1 = new ImageIcon("Data/button1.png");
        final JLabel label1 = new JLabel(icon1);

        final ImageIcon icon2 = new ImageIcon("Data/button2.png");
        final JLabel label2 = new JLabel(icon2);

        final ImageIcon icon3 = new ImageIcon("Data/button3.png");
        final JLabel label3 = new JLabel(icon3);

        final ImageIcon icon4 = new ImageIcon("Data/button4.png");
        final JLabel label4 = new JLabel(icon4);

        final ImageIcon icon5 = new ImageIcon("Data/button5.png");
        final JLabel label5 = new JLabel(icon5);

        final ImageIcon icon6 = new ImageIcon("Data/background.png");
        final JLabel label6 = new JLabel(icon6);

        /*
         * @Image Location
         */
        label.setBounds(282, 255, 96, 96);
        label1.setBounds(384, 255, 96, 96);
        label2.setBounds(282, 153, 96, 96);
        label3.setBounds(384, 153, 198, 96);
        label4.setBounds(181, 152, 96, 96);
        label5.setBounds(181, 255, 96, 96);
        label6.setBounds(0, 0, 765, 503);

        /*
         * @Frame
         */
        final JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(765, 503));
        frame.setLayout(null);
        frame.add(label);
        frame.add(label1);
        frame.add(label2);
        frame.add(label3);
        frame.add(label4);
        frame.add(label5);
        frame.add(label6);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

Solution

  • It comes down to a number of requirements. The main one "who is responsible for the management". You could implement it so that each label becomes responsible for it's own layout management or that the container is responsible.

    Each has there own merits. I prefer the container managed approach, because it does not lock you into just been able to use labels.

    You will want to familiarize your self with the MouseMotionListener and the MouseListener.

    The basic idea is to monitor when a click occurs (and also what was clicked) and using the click point, determine the offset when the mouse is dragged.

    Update

    Sorry, took me some time to find, but I answered a question that might be of use Object on Image does not move when Image moves.

    It's not exactly the same, but it contains concepts that might help.