Setting Specific JPanel Child to Ignore Layout Manager

297 views Asked by At

I'm making a simple GUI where there are multiple JPanels under a GridBag layout manager. The problem is I want to add a drag-and-drop feature by moving around child JPanels under those in the GridBag layout, but for whatever reason it seems setLocation() is not working. I suspect that the layout manager is interfering with this. I don't want to completely take out the layout manager because I need it to help organize the elements, so is there any way to set a number of specific elements to ignore the layout manager?

1

There are 1 answers

0
Santiago Benoit On BEST ANSWER

Just add a new jPanel with a transparent background (if it's not transparent by default use setOpaque(false)) to your GridBag layout, and set its layout to null:

myPanel.setLayout(null);

Also, set its size to an appropriate value, because null layouts by default shrink to nothing with no coordinates.

myPanel.setMinSize(int x, int y);

Any components added to your overlaying jPanel wilL not be affected by a layout and will be able to be moved around freely.