JLabel mouse events for Drag and Drop?

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.

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 http://stackoverflow.com/questions/5309150 * @see http://stackoverflow.com/questions/2561690 */ 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 mousePt = new Point(W / 2, H / 2); public MouseDragTest() { this. SetPreferredSize(new Dimension(W, H)); this.

SetFont(new Font("Serif", Font. ITALIC, 24)); this. AddMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { mousePt = e.getPoint(); repaint(); } }); this.

AddMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { mousePt. Translate( e.getX() - mousePt. X, e.getY() - mousePt.

Y); repaint(); } }); } @Override public void paintComponent(Graphics g) { super. PaintComponent(g); int w2 = g.getFontMetrics(). StringWidth(TITLE) / 2; g.

DrawString(TITLE, mousePt. X - w2, mousePt. 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); } }); } }.

You're right, the second approach is what I used in some of my programs as well. – Thomas Mar 17 at 16:49.

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();} } } }.

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 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 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 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 at 20:11.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions