6

I want to enable the drag and drop feature over a JLabel by overriding mouse events over it , but when I define the drag and drop in mousePressed event ,the mouseReleased does not take effect on that JLabel. Am I doing something wrong ?

            Thumbnails[I_Loop].setText("1");
            Thumbnails[I_Loop].setTransferHandler(new TransferHandler("text"));
            Thumbnails[I_Loop].addMouseListener( new MouseAdapter() {
                public void  mouseReleased(MouseEvent me) {
                       System.out.println("here mouse released");
                  }
                public void mousePressed(MouseEvent me) {
                    System.out.println("here mouse pressed");
                    JComponent comp = (JComponent) me.getSource();
                    TransferHandler handler = comp.getTransferHandler();
                    handler.exportAsDrag(comp, me, TransferHandler.COPY);
            });

*Thumbnails is array of JLabel

When running the program , the drag and drop works but the statement "here mouse released" does not get printed. However, When I remove the code responsible for DND from the mousePressed() method, "here mouse released" is printed.

What is the wrong in this code?

3
  • 1
    what does "does not take effect on same JLabel" mean ?
    – dm76
    Mar 15, 2011 at 8:34
  • i mean that nothing happed when i released the mouse over the JLabel, the statement "here mouse released" does not printed
    – ama
    Mar 15, 2011 at 8:39
  • 1
    It's difficult to see what's wrong without a minimal code that compiles and runs. Could you provide a SSCCE code so we can test and see for ourselves? see sscce.org
    – dm76
    Mar 15, 2011 at 8:55

3 Answers 3

13

@Thomas is correct, but two alternatives are worth noting:

  • This example shows how to drag a component using JLayeredPane; this variation expands on the concept; this more recent example uses a similar approach.

  • The code below shows how to use a MouseMotionListener; this more complex example uses the same principle.

Code:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/5312702/230513 */
public class MouseDragTest extends JPanel {

    private static final String TITLE = "Drag me!";
    private static final int W = 640;
    private static final int H = 480;
    private Point textPt = new Point(W / 2, H / 2);
    private Point mousePt;

    public MouseDragTest() {
        this.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 32));
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                mousePt = e.getPoint();
                repaint();
            }
        });
        this.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                int dx = e.getX() - mousePt.x;
                int dy = e.getY() - mousePt.y;
                textPt.setLocation(textPt.x + dx, textPt.y + dy);
                mousePt = e.getPoint();
                repaint();
            }
        });
    }

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int w2 = g.getFontMetrics().stringWidth(TITLE) / 2;
        g.drawString(TITLE, textPt.x - w2, textPt.y);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame(TITLE);
                f.add(new MouseDragTest());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}
2
  • You're right, the second approach is what I used in some of my programs as well.
    – Thomas
    Mar 17, 2011 at 16:49
  • 1
    +1, nice example to show how to drag a custom painting as oppose to a real component.
    – camickr
    Mar 10, 2013 at 19:03
6

Well, if I remember correctly, the drag and drop machinery catches all mouse events and processes them itself. Thus, the normal MouseEvents are not thrown anymore. You'd need to register a DropTargetListener on the JLabel's DropTarget.

0

Does it have to be a JLabel? I made a class with a string that might get you started..

import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class mainProgram extends JPanel implements Runnable
{
    private static final long serialVersionUID = 1L;
    public static boolean MOUSE_DOWN = false;
    public static String str;

    public mainProgram() 
    {
        JFrame win = new JFrame("Window");

        win.add(this);

        win.setSize(700,500);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);
        str = "Drag me!";
        new Thread(this).start();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        if(MOUSE_DOWN)
        {
            g.drawString(str, MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
        }
    }

    @Override
    public void run() 
    {
        Thread t = Thread.currentThread();

        this.addMouseListener(new MouseListener()
        {

            @Override
            public void mouseClicked(MouseEvent arg0) 
            {

            }

            @Override
            public void mouseEntered(MouseEvent arg0) 
            {

            }

            @Override
            public void mouseExited(MouseEvent arg0) 
            {

            }

            @Override
            public void mousePressed(MouseEvent arg0) 
            {
                MOUSE_DOWN = true;
            }

            @Override
            public void mouseReleased(MouseEvent arg0) 
            {
                MOUSE_DOWN = false;
            }

        });

        while(t==Thread.currentThread())
        {
            if(MOUSE_DOWN)
                repaint();

            try {Thread.sleep(10);}
            catch (InterruptedException e) {e.printStackTrace();}
        }   
    }
}
4
  • I tried your class ,and it is making something beautiful ,i need for emotion while dragging ,but im deal with JLabel contains image inside it
    – ama
    Mar 15, 2011 at 10:30
  • This approach is worth noting, but the implementation has significant problems. In general, it does not use threads correctly, particularly the event dispatch thread. I have suggested an alternative.
    – trashgod
    Mar 15, 2011 at 13:55
  • Nice work improving my code. We still have the problem that it should only react when the mouse starts to drag when the mouse is over the text.
    – Afra
    Mar 15, 2011 at 14:11
  • The FontMetrics getStringBounds() method(s) return a Rectangle2D suitable for use in mousePressed(). The example cited shows an approach for multiple selections. BTW, you need to use @trashgod for me to see a comment on your answer.
    – trashgod
    Mar 15, 2011 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.