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 panel.setSize() Not Working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "panel.setSize() Not Working" Watch "panel.setSize() Not Working" New topic

panel.setSize() Not Working

chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

I have a Frame of Size 300 X 300. In this frame i have two panels, one of size 300 x 100 and other of 300 x 200. The first panel has a Button in the Center while the other panel has 9 buttons arranged in a Grid.

For doing this, i am creating a frame with size 300 x 300 and two panels, one with size 300 x 100 and other with 300 x 200. The problem is when i use panel.setSize(), it is not creating any effect. Below is the code that i have written.

Where am i going wrong?

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10828

GridLayout totally ignores the component's size.
From the API docs for GridLayout
The container is divided into equal-sized rectangles, and one component is placed in each rectangle

To fix your problem,
1) Change from GridLayout to FlowLayout
2) For the panels, use setPreferredSize() instead of setSize().

PS. In your code you haven't set the preferred size for the first panel.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: panel.setSize() Not Working
It's not a secret anymore!