Search code examples
javaswingjpanellayout-managernull-layout-manager

Remove Panel from layout only


I have a frame where the layout is null. There is a panel attached to it that has a flow layout.

My code has it so that a button press creates a new panel that gets added to the first panel (the one attached to the frame). Then I have a mouse listener that lets you drag the newly created panel around.


panel.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent me) {
            x = me.getX();
            y = me.getY();
    }
});
panel.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseDragged(MouseEvent me) {
        me.translatePoint(me.getComponent().getLocation().x-x, me.getComponent().getLocation().y-y);
        panel.setLocation(me.getX(), me.getY());
    }
});

However, when I press the button for it to create a new panel, it creates a new panel while returning the it to the flow layout. I've tried removing the panel but when I drag the new panel over it, it gets erased. While if I revalidate and repaint the panel after removing it, it vanishes.

So how do I prevent it from getting erased or remove it from the layout only?


Solution

  • Try changing the layout to null, I don't think what you're asking is possible.