Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding padding to GUI components

 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a border layout, and each cell except SOUTH in the layout contains a JPanel. Each JPanel contains a few components. All I would like to do is add some padding to the NORTH and SOUTH JPanels and keep them centered....

Or, I want to take this:


to this:


How can I add padding either to the JPanels in the NORTH and SOUTH cells, or to the BorderLayout cells?

Of course that really wasn't code, but it was the only way I could find to format it correctly.
[ October 18, 2004: Message edited by: Matthew Taylor ]
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pad every cell in a BorderLayout by using the constructor
new BorderLayout(5,5) where 5 is the px of padding you want on every cell.

You can also pad components individually by giving them an empty border (or compound border if you need the line).
myComp.setBorder(BorderFactory.createEmptyBorder(top,left,bottom,right));

Cheers,
Rachel
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should have known. Thanks, the EmptyBorder did what I wanted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic