Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adjust border margins/size for JPanel  RSS feed

 
Alex Sanchez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex Sanchez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could either set gaps between the panels in ur BorderLayout or u could explicitly set insets.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!