I have added some components to JPanel
which is set to grid layout, and I'm adding this to JFrame
which is set to border layout. But I want to fix the size of the panel. When my window is maximized, the size of all components are increasing. I want the panel at the center of the window, with fixed size even if window is maximised.
Asked
Active
Viewed 1.0k times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

user1415043
- 7
- 1
- 1
- 1
-
*"please help me with some coding"* Please help yourself to a question & any sign of effort. SO is not a code generation machine. BTW - Don't use code formatting for text, and check the post formats as you expect in the preview. Voting to close. – Andrew Thompson May 25 '12 at 06:28
-
If you provide some code, it will be better. For example, I would look in your code to see if and where you call `setPreferredSize`. – Radu Murzea May 25 '12 at 06:29
-
If the panel contains human readable text, _don't do this_! Font metrics vary by platform. – trashgod May 25 '12 at 14:17
3 Answers
3
Put the panel with GridLayout
as a single component to a GridBagLayout
with no constraint - it will be centered. Add the panel with GBL to the CENTER
of the BorderLayout
.
See this example for the above image.
The Nested Layout Example also uses GBL to center the image in the lower part of the scroll-pane on the right.

Community
- 1
- 1

Andrew Thompson
- 168,117
- 40
- 217
- 433
1
Well then you should not use BorderLayout, because that just fits the child components in. If you still want to use BoderLayout on the JFrame (because you need some side panel or something like that), You can just wrap your JPanel (with the GridLayout) into another JPanel with a GridBagLayout or BoxLayout or something similar and then put that other JPanel into the JFrame.
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout());
// fill and set your innerPanel
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new GridBagLayout());
middlePanel.add(innerPanel, constraintsThatPlaceItWhereYouWantIt);
JFrame yourFrame = new JFrame();
yourFrame.setLayout(new BorderLayout());
yourFrame.add(middlePanel, BorderLayout.CENTER);

brimborium
- 9,362
- 9
- 48
- 76
-
`middlePanel.add(innerPanel, constraintsThatPlaceItWhereYouWantIt);` See my answer for why `constraintsThatPlaceItWhereYouWantIt` is redundant for centering a component in GBL. It links to example code. – Andrew Thompson May 25 '12 at 06:38
-
You are right, but I still always give constraints when adding, because of readability. And he might do some more advanced stuff with it regarding placement. – brimborium May 25 '12 at 06:41
-
-
How about the last line: `yourFrame.add(middlePanel, BorderLayout.CENTER);`? Would you skip the `BorderLayout.CENTER` as well? I wouldn't although it is not neccessary. – brimborium May 25 '12 at 06:47
0
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JPanel rootPanel = new JPanel();
frame.getContentPane().add(rootPanel, BorderLayout.CENTER);
rootPanel.setLayout(new GridBagLayout());
JPanel contentPanel = new JPanel();
Dimension dimension = new Dimension(300, 300);
contentPanel.setMaximumSize(dimension);
contentPanel.setMinimumSize(dimension);
contentPanel.setPreferredSize(dimension);
contentPanel.setBackground(Color.YELLOW);
GridBagConstraints g = new GridBagConstraints();
g.gridx = 0;
g.gridy = 0;
g.anchor = GridBagConstraints.CENTER;
rootPanel.add(contentPanel, g);
frame.setVisible(true);

Liu guanghua
- 981
- 1
- 9
- 18
-
1) Use code formatting 2) This is rarely, if ever, the answer. -1 – Andrew Thompson May 25 '12 at 06:33
-