aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Forcing size of JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Forcing size of JPanel" Watch "Forcing size of JPanel" New topic
Author

Forcing size of JPanel

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
I'm trying to create a color panel behind some JLabels for visual feedback on a process. The problem I'm having is that the panel automatically resizes to fit the labels, regardless of any of the size settings.

This is a data collection program running on our factory floor collecting data from some wireless barcode scanners. The labels provide detailed status of the process is you are standing near the monitor, but the colored panel provides feedback the operators can see across the shop floor.

I've sucessfully set the size of the labels based on a percentage of the parent JFrame, but even if I set the color panel to 90% of the frame width, it automatically downsizes to just contain the labels.

Is there any way to force a panel to be certain size and not automatically shrink to fit the contained elements? Or, is there some other way to do what I'm trying to do?

TIA,
[ January 27, 2005: Message edited by: Tom Blough ]

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hi Tom,


Best regards,


Lionel Badiou
CodeFutures Software
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
oops ;-)

You may look at the setPreferredSize(java.awt.Dimension) method. look
here for details.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Yup, already tried setPreferredSize and setMinimumSize. Panels seem to ignore these and automatically resize to fit the contained items.

Cheers,
[ January 27, 2005: Message edited by: Tom Blough ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I think your problem is actually with your LayoutManager.

You can set a panels layout manager to null.

Do that, and then set the size of your panel to whatever you like. Just remember that any components you place on that panel will now have to have their locations specified also.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Thanks, Jefff. That does work, but is a PITA. Are there any layout managers that do not pack the components before displaying, or any way to disable packing for a given layout manager?

TIA,
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Not that I know of.

It's less of a PITA if you use a IDE that has some sort of graphical design module.

I use JBuilder and it seems to easy enough to get along with.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
have you included a frame.pack() or just pack() (if extending JFrame)
this will (generally) set everything to their preferred size
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forcing size of JPanel