-1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zhang Ye
  • 3
  • 1
  • 2
    Please create and post your [mcve] – Hovercraft Full Of Eels Mar 12 '18 at 20:57
  • 2
    `"frame where the layout is null"` -- note that this is usually a very bad idea. Usually it's better to use layouts and to drag images or shapes, not components. Also don't forget to call `repaint()` on the container after changing any components within it. You would also call `revalidate()` if the container uses a layout manager. – Hovercraft Full Of Eels Mar 12 '18 at 21:00
  • You verbal description of the problem isn't clear to me. If you have a JPanel with buttons that uses a FlowLayout, then the layout of the frame should be a BorderLayout. Then you add this panel to the NORTH/SOUTH depending on your requirement. Then you create a second panel with a null layout that you add to the CENTER of the frame. Then when you click the button you add the new panel to this panel in the center. – camickr Mar 13 '18 at 01:02
  • You may want to consider using the [Drag Layout](https://tips4java.wordpress.com/2011/10/23/drag-layout/) for the CENTER panel. It provides some layout functionality while allowing you to drag components to a new location. – camickr Mar 13 '18 at 01:03

1 Answers1

0

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

Alex
  • 16