Re: Draggable lables/button on frame/panel




Chanchal wrote:
Hello All,
in an appliction i'm developing, i need to place jlabels or button on
a frame/panel. when the application is running, i should be able to
drag these jlabels/buttons around. how can this be done. Please advice


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LabelTest
{
private JFrame frame;
private JPanel panel = new JPanel();
private JLabel label = new JLabel("Label");

public LabelTest()
{
frame = new JFrame("LabelTest");
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.addMouseMotionListener(new DragHandler());
panel.add(label);
frame.add(panel);
frame.setVisible(true);
}

class DragHandler extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
Component c = e.getComponent();
c.setLocation(c.getX() + e.getX(), c.getY() + e.getY());
frame.repaint();
}
}

public static void main(String[] args)
{
LabelTest test = new LabelTest();
}
}


.