Hi all!

Could someone pliz explain me how to delete a component (in my case -> JLabel that is placed on JLayeredPane), when it is double-clicked? So, as far as I understand, I must first just detect a componment that is double-clicked and then delete it. Although an idea is simple, my code doesn't work correctly:

public void mouseClicked(MouseEvent e) 
    {
        if (e.getClickCount() == 2)  
        {
            this.layeredPane.remove(e.getComponent());
            this.layeredPane.repaint();
            System.out.println(e.getSource());
        }
    }

Pliz helppp!:'(

Thx!

my code doesn't work correctly:

Can you describe what isn't correct about what you've posted?

To debug your code add a

System.out.println("e=" + e); // Show event contents

to the listeners and click/double-click the component and look at what is printed out.

Edited 6 Years Ago by NormR1: n/a

I wrote a testing program as follows. If I clicked twice the label is deleted. Perhaps for the line 5:
this.layeredPane.remove(e.getComponent());
your may replace e.getComponent() with actual reference: "label".

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

public class DeleteComponent extends JFrame {	
JLabel label;
public DeleteComponent(){
	Container container = getContentPane();
	label = new JLabel("The label to be deleted");
	container.add(label);
	setVisible(true);
	setSize(150,100);
	container.addMouseListener(new MouseAdapter(){
		public void mouseClicked(MouseEvent e) {	
  		if (e.getClickCount() == 2) {
            remove(label);
            repaint();
        }
    }});
}

public static void main(String args[]){
	DeleteComponent dc = new DeleteComponent();
	dc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

Edited 6 Years Ago by tong1: n/a

Hi tong1!

Thank you for the code. But the problem is that I can't write "remove(label)", because there are many-many JLabel components on my JLayeredPane. I must somehow determine which JLabel component has been double clicked. Do you know how it can be done?

Hi NormR1!

I will post the debug message today, but a bit later.

the problem becomes the method of MouseEvent:
getComponent()
which Returns: the Component object that originated the event, or null if the object is not a Component.
This method cann't identify the actually component clicked.

Ok, thanks. Does it mean that this problem is unsolvable? Maybe some tricks...?

the problem becomes the method of MouseEvent:
getComponent()
which Returns: the Component object that originated the event, or null if the object is not a Component.
This method cann't identify the actually component clicked.

I wrote my version using your code. I used the JPanel class. I declared it as an attribute of the class so I can have access to it in the method that handles the double clicking.

package stam.bar;

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

public class SimpleFrame extends JFrame implements MouseListener {
    private JLabel lb1 = new JLabel("Hi 1");
    private JLabel lb2 = new JLabel("Hi 2");
    private JLabel lb3 = new JLabel("Hi 3");

    private JPanel panel = new JPanel();
    
    public SimpleFrame() {
        setSize(300, 300);
        
        lb1.addMouseListener(this);
        lb2.addMouseListener(this);
        lb3.addMouseListener(this);
        
        panel.add(lb1);
        panel.add(lb2);
        panel.add(lb3);
        
        add(panel);
    }

    public static void main(String[] args) {
        SimpleFrame simpleFrame = new SimpleFrame();
        simpleFrame.setVisible(true);
    }
    
    public void mouseClicked(MouseEvent e) 
    {
        if (e.getClickCount() == 2)  
        {
            if (e.getComponent()!=null) {
                panel.remove(e.getComponent());
                repaint();
            }
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}

I my version I add the mouse listeners directly to the components: lb3.addMouseListener(this);

Edited 6 Years Ago by javaAddict: n/a

Comments
thanks.

Hi javaAddict!

Thank you for the code, it's working! I've decided to use it inside the class "Listener".

Now the problem is with the "mouseDragged" procedure that is required to change the position of a component by mouse dragging. In order to see if this procedure is executable, I've included "System.out.print("dragged "). However the message "dragged " doesn't appear, which means that mouseDragged is not executed. What could be the problem?:-/ The most interesting is that the "mouseDragged" procedure worked before putting it in the class "Listener". I, however, want it to be exactly inside this class.

public class LabelDragAndDrop extends JFrame
{
...
labelclon.addMouseListener(new Listener(labelclon));
...
}
public class Listener extends MouseAdapter implements MouseListener, MouseMotionListener{
        public Listener(JLabel l){
            super();
            addMouseListener(this);
            addMouseMotionListener(this);
        }
        public void mouseClicked(MouseEvent e){
            if (e.getClickCount() == 2)
            {
                if (e.getComponent()!=null) {
                   layeredPane.remove(e.getComponent());
                   repaint();
                }
            System.out.print("double clicked ");
            }
        }
        public void mousePressed(MouseEvent e)
        {
            dragComponent = null;
            Component c = layeredPane.findComponentAt(e.getX(), e.getY());
            if (c instanceof JLayeredPane) return;
            dragComponent = c;
            xAdjustment = dragComponent.getLocation().x - e.getX();
            yAdjustment = dragComponent.getLocation().y - e.getY();
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
            layeredPane.moveToFront(dragComponent);
            System.out.print("pressed ");
            
        }
        /*
        ** Move the component around the panel
        */
        public void mouseDragged(MouseEvent e)
        {
            if (dragComponent == null) return;
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
            System.out.print("dragged ");
        }
        /*
        ** Deselect the component
        */
        public void mouseReleased(MouseEvent e)
        {
            dragComponent = null;
            System.out.print("released ");
        }
   }

Do you have print outs from all the listener methods? Do any of them print when you use the mouse on the component they are listeners for?
Add @Override before all the listener methods to be sure the methods are being overridden.

The MouseAdapter class definition includes the listeners:

public abstract class MouseAdapter
extends Object
implements MouseListener, MouseWheelListener, MouseMotionListener

Edited 6 Years Ago by NormR1: n/a

Hi NormR1!

Thanks! I've added @Override before all listeners, but still the code doesn't work.

Yes, I have print outs for all listener methods except mouseDragged... Hmm.. Any idea?

What do you see printed out when you press a key and move the mouse?

Move the print before the if test so it ALWAYs prints.
Also add ", e=" +e to the end of the print out.

Edited 6 Years Ago by NormR1: n/a

Ok, I've putted System.out.println before IF. The code is:

public class Listener extends MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener{
        public Listener(){
            super();
            addMouseListener(this);
            addMouseMotionListener(this);
        }
        @Override
        public void mouseClicked(MouseEvent e){
            System.out.println("double clicked: " + e);
            if (e.getClickCount() == 2)
            {
                if (e.getComponent()!=null) {
                   layeredPane.remove(e.getComponent());
                   repaint();
                }
            }
        }
        @Override
        public void mousePressed(MouseEvent e)
        {
            System.out.println("pressed: " + e);
            dragComponent = null;
            Component c = layeredPane.findComponentAt(e.getX(), e.getY());
            if (c instanceof JLayeredPane) return;
            dragComponent = c;
            xAdjustment = dragComponent.getLocation().x - e.getX();
            yAdjustment = dragComponent.getLocation().y - e.getY();
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
            layeredPane.moveToFront(dragComponent);            
        }
        /*
        ** Move the component around the panel
        */
        @Override
        public void mouseDragged(MouseEvent e)
        {
            System.out.println("dragged: " + e);
            if (dragComponent == null) return;
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        }
        /*
        ** Deselect the component
        */
        @Override
        public void mouseReleased(MouseEvent e)
        {
            System.out.println("released: " + e);
            dragComponent = null;
        }
   }

The print outs are:

run:
pressed: java.awt.event.MouseEvent[MOUSE_PRESSED,(21,32),absolute(574,143),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JLabel[,10,10,47x44,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@1ddebc3,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
released: java.awt.event.MouseEvent[MOUSE_RELEASED,(21,32),absolute(574,143),button=1,modifiers=Button1,clickCount=1] on javax.swing.JLabel[,10,10,47x44,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@1ddebc3,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
double clicked: java.awt.event.MouseEvent[MOUSE_CLICKED,(21,32),absolute(574,143),button=1,modifiers=Button1,clickCount=1] on javax.swing.JLabel[,10,10,47x44,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@1ddebc3,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
pressed: java.awt.event.MouseEvent[MOUSE_PRESSED,(32,39),absolute(585,150),button=3,modifiers=Meta+Button3,extModifiers=Button3,clickCount=1] on javax.swing.JLabel[,10,10,47x44,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@1ddebc3,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
released: java.awt.event.MouseEvent[MOUSE_RELEASED,(32,39),absolute(585,150),button=3,modifiers=Meta+Button3,extModifiers=Meta,clickCount=1] on javax.swing.JLabel[,10,10,47x44,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@1ddebc3,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
BUILD SUCCESSFUL (total time: 9 seconds)

So, as you may see all listener methods except the mouseDragged have been executed. I cannot understand why the mouseDragged doesn't work, when I'm dragging the mouse.

What do you see printed out when you press a key and move the mouse?

Move the print before the if test so it ALWAYs prints.
Also add ", e=" +e to the end of the print out.

You haven't overridden the mouseMoved method.
Did you add a MouseMotionListener to the component?

Edited 6 Years Ago by NormR1: n/a

A=) ok, I've added a MouseMotienListener:

Listener list = new Listener();
labelclon.addMouseListener(list);
labelclon.addMouseMotionListener(list);

Also, I've overriden the mouseMoved method (but without any code, because before everything has worked with empty mouseMoved method.). I'm not sure that this has a sense:

@Override
public void mouseMoved(MouseEvent e) {}

So, now I can drag the JLabel: labelclon. Thanks so far!...BUT, I can drag it only once. Then it is locked and doesn't react on mouse dragging... Why this could happen?

Edited 6 Years Ago by LianaN: n/a

Do some debugging by adding println()s to show where the component currently is and where you are trying to move it to.


Perhaps you need to tell the component's container about the change: validate for example.

Do some debugging by adding println()s to show where the component currently is and where you are trying to move it to.


Perhaps you need to tell the component's container about the change: validate for example.

I have the following lines of code:

Listener list = new Listener();
labelclon.addMouseListener(list);
labelclon.addMouseMotionListener(list);
this.layeredPane.add(labelclon);
labelclon.setLocation(10, 10);
layeredPane.revalidate();
repaint();

Also, I've updated the mouseDragged:

@Override
        public void mouseDragged(MouseEvent e)
        {
            System.out.println("dragged: " + dragComponent);
            if (dragComponent == null) return;
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        }

So, when I drag for the first time, System.out.println returns:

dragged: javax.swing.JLabel[,297,142,47x44,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=javax.swing.ImageIcon@f5da06,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]

But when I drag for the second time, it returns "null".

it returns "null".

What is the "it" that returns null?

System.out.println("dragged: " + dragComponent);

If its dragComponent, where in your code is that variable set?
Put some println()s next to where it is set to show when it is getting set to null.

dragComponent is inside the class "Listener". I removed "dragComponent = null" from the code. Now I can drag the component multiple times, but this dragging looks somehow strange. I cannot explain it, well, like "a drunk mouse". I cannot normally drag the component, sometimes it occasionally changes its location, etc. I think this happens because of inproper use of dragComponent = null. But I don't have any idea of how to improve the code.

public class Listener extends MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener{
       private Component dragComponent;
       public Listener(){
            super();
            System.out.println("created: " + dragComponent);
            //addMouseListener(this);
            //addMouseMotionListener(this);
        }
        @Override
        public void mouseClicked(MouseEvent e){
            //System.out.println("double clicked: " + e);
            System.out.println("clicked: " + dragComponent);
            if (e.getClickCount() == 2)
            {
                if (e.getComponent()!=null) {
                   layeredPane.remove(e.getComponent());
                   repaint();
                }
            }
        }
        @Override
        public void mouseMoved(MouseEvent e) {
            //System.out.println("moved: " + e);
            System.out.println("moved: " + dragComponent);
        }
        @Override
        public void mousePressed(MouseEvent e)
        {
            //System.out.println("pressed: " + e);
            System.out.println("pressed: " + dragComponent);
            //dragComponent = null;
            Component c = layeredPane.findComponentAt(e.getX(), e.getY());
            if (c instanceof JLayeredPane) return;
            dragComponent = c;
            xAdjustment = dragComponent.getLocation().x - e.getX();
            yAdjustment = dragComponent.getLocation().y - e.getY();
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
            layeredPane.moveToFront(dragComponent);            
        }
        /*
        ** Move the component around the panel
        */
        @Override
        public void mouseDragged(MouseEvent e)
        {
            System.out.println("dragged: " + dragComponent);
            if (dragComponent == null) return;
            dragComponent.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        }
        /*
        ** Deselect the component
        */
        @Override
        public void mouseReleased(MouseEvent e)
        {
            //System.out.println("released: " + e);
            System.out.println("released: " + dragComponent);
            //dragComponent = null;
        }
   }

I've updated "Component c = layeredPane.findComponentAt(e.getComponent().getX(), e.getComponent().getY());" and now the mouse dragging works better. But the mouse cursor is always located far from the image that I drag. That's pretty strange.

It takes a bit or work to get the X,Y values right. Keep going, you should be able to have the cursor close to the same location on the component as the component is being moved.

This article has been dead for over six months. Start a new discussion instead.