Forums Register Login

Adjust border margins/size for JPanel

+Pie Number of slices to send: Send
Hello
I got two Jpanels on top of each other, the upper panel has a TitledBorder(EtchedBorder) and the lower one has simply an EtchedBorder. The thing is that because the upper has a title the margins are wider than the lower one. I want them to have the same margins. the same size, not as in the picture below

Has this something to do with Insets? any good solution?
[ March 21, 2002: Message edited by: Alex Sanchez ]
+Pie Number of slices to send: Send
Hi Alex,
Yes you are correct. The titledBorder adds an insets value of 2 to the normal border on the non title edges.
You could place your panels into a GridBagLayout and set the insets to 2 to the untitled panel. The code below shows a simple example.

Regards,
Manfred
+Pie Number of slices to send: Send
Thanks Manfred!
It worked perfect!
By the way, does this only work with GridBagLayout? Because right now i'm adding the two panels to a panel that has a BorderLayout(the upper to "North", and the lower to "Center")..
Thanx again.
+Pie Number of slices to send: Send
You could either set gaps between the panels in ur BorderLayout or u could explicitly set insets.
+Pie Number of slices to send: Send
Hi Alex,
Unfortunately, I don't know of any other layout that allows the setting of insets. When I have needed it on a BorderLayout I have had to extend the BorderLayout and override the getInsets method. Then I could place anything I wanted into the insets and also create a setInsets method similar to the GridBagConstraints class already has done. If you are going to make a habbit of adjusting the insets you might want to take this approach and create a InsetBorderLayout or something like that.
You could also use a FlowLayout which allows you to directly set margins. Each panel would then need to be placed into a FlowLayout so that you could adjust the margins (i.e., 0 for titled and 2 for untitled).
Regards,
Manfred.
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 37069 times.
Similar Threads
Swing event handling for a JButton?
newbie multiple frames problem
upload file through applet
How to format a string it title case???
Calling a method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:17:20.