File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem with GridLayout. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with GridLayout." Watch "Problem with GridLayout." New topic

Problem with GridLayout.

Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Hi All,

I created a Panel(ex: TopPanel) and set the layout of the panel to GridLayout(1,2).
And the panel (TopPanel) has two components. The components are also other panels(ex: TopPanel1 and TopPanel2).
Actually the problem is--- "I need to set the first component(TopPanel1) width to 75% of the TopPanel and other 25 % of the will take the TopPanel2".
I am using GridLayout, so the components (cell) will get equal width and height.
Is there any way to solve my problem with the GridLayout.
I not very flexible in using GridBagLayout, so that I need to with GridLayout.

Thanks In Advance.

Renjith M :

Personal E-mail:
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

You said it yourself: GridLayout will divide the space equally between the components. You can't change that, that's just how it works.

Apart from GridBagLayout, I can't think of any layout manager that can do this. However, GridBagLayout is not that hard for this:
The only problem with this code is that the 75% - 25% will only kick in after the frame is resized enough. That's because it uses the preferred size of the buttons as the initial size. You can fix that by calling setPreferredSize on your components.

How To Ask Questions How To Answer Questions
Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Thanks Rob.

Thank you very much for your time and help.

I used the code and set the preferredSize() method and I got the solution.

Renjith M
I agree. Here's the link:
subject: Problem with GridLayout.
It's not a secret anymore!