Adding a JLabel to JLayeredPane from event listener not dragging.

Posted by Cody on Stack Overflow See other posts from Stack Overflow or by Cody
Published on 2010-04-24T05:16:30Z Indexed on 2010/04/24 5:23 UTC
Read the original article Hit count: 372

Filed under:
|

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;

© Stack Overflow or respective owner

Related posts about java

Related posts about swing