0

I'm trying to add a JLabel to a JComponent (which is already on a JPanel), then making it so that I can click and drag the JLabel to reposition it somewhere different.

At the moment the JLabel does appear but when I click and drag it, it moves the whole JComponent instead of just the JLabel on its own.

Here is an example of the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.*;

public class Test extends JComponent implements ActionListener {
    int dragX, dragY;
    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;
        g2.drawRect(0, 0, 300, 300);

        MouseListener ml = new MouseListener() {
            public void mousePressed(MouseEvent e) { 
                dragX = e.getX();
                dragY = e.getY();
            }
        };

        MouseMotionAdapter mma = new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e){
                setLocation(e.getX() - dragX + getLocation().x,
                        e.getY() - dragY + getLocation().y);
            }
        };

        BufferedImage image;
        try {
            image = ImageIO.read(new File("image.png"));
            ImageIcon i = new ImageIcon(image);
            JLabel label = new JLabel(i);                                               
            label.setBounds(30, 30, 60, 19);
            label.addMouseListener(ml);
            label.addMouseMotionListener(mma);
            add(label);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900, 650);
        frame.setVisible(true);
        frame.setResizable(false);
        Test t = new Test();
        frame.getContentPane().add(t);
    }
}

I feel like it has something to do with the setLocation() line in the MouseMotionAdapter() block, but I'm not entirely sure.

Thanks in advance!

Seb
  • 1
  • 1
  • Consider using a JLayeredPane or even the glasspane to help you move the label around. [For instance](http://stackoverflow.com/a/4894516/522444). – Hovercraft Full Of Eels Feb 22 '16 at 22:11
  • 1
    Well, to start with, don't do it all within the `paintComponent` method, that's just asking for trouble – MadProgrammer Feb 22 '16 at 22:12
  • Or [this example](http://stackoverflow.com/a/4687759/522444) may even be better. – Hovercraft Full Of Eels Feb 22 '16 at 22:15
  • Oh my, I didn't realize that you were doing all of that within paintComponent, including creating and adding MouseListeners, reading in image files, creating and placing components. Please understand that @MadProgrammer's statement was a gross understatement, that doing this sort of thing inside of paintComponent isn't asking for trouble, it's asking for a complete disaster. Just use paintComponent for painting and painting only, and don't forget to call the super's paintComponent within your override. – Hovercraft Full Of Eels Feb 22 '16 at 22:17
  • Not sure why everybody is using a JLayeredPane or a GlassPane to drag the label. The label need to eventually be located on the JPanel anyway, so why not just drag it around the panel? Just use a null layout on your panel and you can use the `DragListener` contained in the [Moving Windows](https://tips4java.wordpress.com/2009/06/14/moving-windows/) example. It is basically the code you have now except the logic is in a separate class. I certainly would not recommend the answer suggested as the duplicate. – camickr Feb 22 '16 at 22:29

0 Answers0