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

Look at standard java \jdk1.7.0\demo\applets\GraphLayout folder
used variables:
- boolean pickfixed;
- int numMouseButtonsDown;
and methods:
- removeMouseMotionListener(this);
- addMouseMotionListener(this);

or alternatively in place of clicked use left and right button (with key modifiers)

what is mouseClicked?
mousePressed
mouseReleased
mousePressed
mouseReleased

Test this on simple mouse program similar to line 22

Ok, thanks. I'll try your variants a bit later. Now I want to arrange all details with my code snippet. After some experiments, I found out that the problem occurs when I try to drag JLabel component outside of the JLayeredPane area. I tried "try..catch", but it did not help.

@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;
            try {
              xAdjustment = dragComponent.getLocation().x - e.getX();
              yAdjustment = dragComponent.getLocation().y - e.getY();
              dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
              MainClass.getBackgroundPane().moveToFront(dragComponent);
            } catch (Exception ex) {
                dragComponent.setLocation(e.getComponent().getX(), e.getComponent().getY());
                MainClass.getBackgroundPane().moveToFront(dragComponent);
            }
        }

So, how could I prevent this error?

Well, now it's working. But the problem is that when I add multiple JLabel components on JPanel, then I can drag and drop only the last one (that is on top). Someone knows how to solve this problem??!

public class DragDropListener extends MouseAdapter implements MouseListener, MouseMotionListener
{
	Component dragComponent;
	int xAdjustment;
	int yAdjustment;

	public DragDropListener() {}

        @Override
	public void mousePressed(MouseEvent e)
	{
		dragComponent = null;
		Component c =  MainClass.getBeadBoard().findComponentAt(e.getX(), e.getY());

		if (c instanceof JPanel) return;

		dragComponent = c;
		xAdjustment = dragComponent.getLocation().x - e.getX();
		yAdjustment = dragComponent.getLocation().y - e.getY();
		dragComponent = (JLabel)c;
		dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
		dragComponent.setSize(dragComponent.getWidth(), dragComponent.getHeight());
		MainClass.getBackgroundPane().add(dragComponent, JLayeredPane.DRAG_LAYER);
	}

        @Override
	public void mouseDragged(MouseEvent me)
	{
		if (dragComponent == null) return;

		dragComponent.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
	 }

        @Override
	public void mouseReleased(MouseEvent e)
	{
		if (dragComponent == null) return;

		//dragComponent.setVisible(false);
		Component c =  MainClass.getBeadBoard().findComponentAt(e.getX(), e.getY());

		if (c instanceof JLabel)
		{
			Container parent = c.getParent();
			parent.remove(0);
			parent.add( dragComponent );
		}
		else
		{
			Container parent = (Container)c;
			parent.add( dragComponent );
		}

		dragComponent.setVisible(true);
	}

        @Override
	public void mouseClicked(MouseEvent e) {}
        @Override
	public void mouseMoved(MouseEvent e) {}
        @Override
	public void mouseEntered(MouseEvent e) {}
        @Override
	public void mouseExited(MouseEvent e) {}

}

I changed mouseReleased method, and now I can add multiple JLabel components to JPanel and drag them separately:

@Override
	public void mouseReleased(MouseEvent e)
	{
		if (dragComponent == null) return;
	Component c =  MainClass.getBeadBoard().findComponentAt(e.getX(), e.getY());
                Container parent = (Container)c;
		parent.add( dragComponent );
		dragComponent.setVisible(true);
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.