Hello ,,,
am having trouble while making a drag and drop label
i wrote a code i'v seen on youtube , but after compiling i had punsh of Exception and i don't know why !!
can any one help me writing a drag and drop code , i'll post the code i'v wrote ,,
project about dragging label and drop it in another place on frame ...
Code :
import java.awt.event.*;
import javax.swing.*;
public class mouseDragandDrop extends JFrame implements MouseListener , MouseMotionListener {
private boolean drag = false ;
private JLabel lbl ;
public mouseDragandDrop ()
{
try
{
lbl = new JLabel ("drag & drop");
lbl.setBounds(0, 0, 100, 20);
lbl.addMouseListener(this);
lbl.addMouseMotionListener(this);
add(lbl);
}
catch (Exception e)
{
System.out.println("Error is : " + e);
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
drag =true ;
}
@Override
public void mouseReleased(MouseEvent e) {
drag = false ;
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseDragged(MouseEvent e) {
if(drag == true )
{
lbl.setBounds(lbl.getBounds().x+e.getX(),lbl.getBounds().y+e.getY(), 100, 20);
}
}
@Override
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public static void main (String args [])
{
mouseDragandDrop m = new mouseDragandDrop ();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(400, 400);
m.setLocationRelativeTo(null);
m.setVisible(true);
}
}
i think that the proplem is in the drag code :
public void mouseDragged(MouseEvent e) {
if(drag == true )
{
lbl.setBounds(lbl.getBounds().x+e.getX(),lbl.getBounds().y+e.getY(), 100, 20);
}
}
thx for your help in advance ...`