Reputation: 63
I am using a GridBagLayout
for part of my program. When I add this part of the interface to the program it resizes the rest of the interface and makes it look silly.
Here is a side-by-side of the interface with and without the grid bag layout added to it.
As you can see, adding the grid bag layout object to the interface resizes the other objects in the frame.
Below is what I'm hoping to have the interface look like:
(I made this really quickly in MS Paint, not with code.)
Do I need to switch the entire interface over to a grid bag layout, or is it possible to do this without that?
Upvotes: 1
Views: 68
Reputation: 324118
The problem is NOT the GridBagLayout.
The problem is the layout manager you use on the parent panel (it looks to me like you might be using a GridLayout) and on the panel used in the left part of the frame.
The issue is that the preferred height of the panel on right is greater than the preferred height of the panel on the left. The layout manager you are using is then adding the extra height to all the components on the left panel. Again, you don't tell us what layout manager you are using, but I would guess GridLayout or BoxLayout, and the layout manager is resizing components to fill the extra space available.
So the solution is to use the layout manager more effectively.
So I would keep the default BorderLayout of the frame and then your basic logic would be something like:
JPanel rightPanel = new JPanel(); // use your GridBagLayout.
frame.add(rightPanel, BorderLayout.CENTER);
JPanel leftPanel = new JPanel(); // use you current layout
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(leftPanel, BorderLayout.PAGE_START);
frame.add(wrapper, Border.LINE_START);
Now the preferred height of the leftPanel will be respected by the wrapper panel. So even if the rightPanel is greater, the components won't be stretched.
Upvotes: 2