tags:

views:

55

answers:

1

Ok, So for some reason When I add components to a JLayeredPane in its constructor:

JLabel label = new JLabel();

label.setSize(100,100); label.setText("This works"); add(label); It works perfectly fine, but If a add it later in the JLayeredPane's parent EDT it doesnt let me move the objects around but they let me see the objects.

Adding from EDT:

JLabel label = new JLabel();
label.setToolTipText(url.getHost());
label.setIcon(icon);
label.setBorder(new LineBorder(null));
label.setSize(icon.getIconWidth(), icon.getIconHeight());

dressFrame.layeredPane.add(label, JLayeredPane.DRAG_LAYER);

Dragging method:

Component c = findComponentAt(e.getX(), e.getY());
            if (c instanceof JLayeredPane) {
                    pieceSelected = false;
                    return;
            }

            Point parentLocation = c.getLocation();
            xAdjustment = parentLocation.x - e.getX();
            yAdjustment = parentLocation.y - e.getY();

            movingPiece = c;
            movingPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
            pieceSelected = true;

http://xslayer.com/upload/gui.png

A: 

It isn't clear from your code snippet how your dragging method gets invoked.

Perhaps you should be registering a MouseMotionListener (with your movement code in the mouseDragged event handler) to the JLabel.

suihock
The class implements MouseListener, MouseMotionListener, and MouseWheelListener. I know the moving works, but it only works when you add the Component in the class's constructor for some reason.
Cody
You still have to add the listeners to the component.
camickr
How do I do that?
Cody
<component to listen on>.addMouseMotionListener(<listening class>)
suihock