Hi!
I'm trying to add dragging and dropping behaviour to JLabel. It works only when I press the mouse for the first time. If I continue pressing the mouse, the error message shown below is generated. Please, help me to find an error.
JLabel bkg = new JLabel(icon);
jLayeredPane.add(bkg, new Integer(2), 0);
DragDropListener ddList = new DragDropListener();
bkg.addMouseListener(ddList);
bkg.addMouseMotionListener(ddList);
...
public class DragDropListener extends MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener {
public Component dragComponent;
private int xAdjustment;
private int yAdjustment;
public DragDropListener(){
super();
//addMouseListener(this);
//addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e){
System.out.println("clicked: " + dragComponent);
if (e.getClickCount() == 2)
{
if (e.getComponent()!=null) {
MainClass.getBackgroundPane().remove(e.getComponent());
MainClass.getBackgroundPane().repaint();
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e)
{
dragComponent = null;
Component c = MainClass.getBackgroundPane().findComponentAt(e.getComponent().getX(), e.getComponent().getY());
if (c instanceof LayeredPane) return;
dragComponent = c;
xAdjustment = dragComponent.getLocation().x - e.getX();
yAdjustment = dragComponent.getLocation().y - e.getY();
dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
MainClass.getBackgroundPane().moveToFront(dragComponent);
}
@Override
public void mouseDragged(MouseEvent e)
{
if (dragComponent == null) return;
dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
}
@Override
public void mouseReleased(MouseEvent e)
{
dragComponent = null;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swarovsky_api_pack.DragDropListener.mousePressed(DragDropListener.java:56)
at java.awt.Component.processMouseEvent(Component.java:6260)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Line 56 in the DragDropListener class is the following:
xAdjustment = dragComponent.getLocation().x - e.getX();