Suhail Gupta
Suhail Gupta

Reputation: 23256

setting horizontal and vertical margins

What is the method to set horizontal and vertical margins in a panel? (The same we have in html style="margins:30px")

Upvotes: 18

Views: 44792

Answers (5)

Suhail Gupta
Suhail Gupta

Reputation: 23256

setBorder(BorderFactory.createEmptyBorder(int top, int left, int bottom, int right));

And for more details you can read the documentation about BorderFactory

Upvotes: 39

kleopatra
kleopatra

Reputation: 51525

Don't know html, so just guessing on possible equivalents :-)

  • to set some space between a component's bounding rectangle and its content, the property to set is its Border
  • the spacing between the different components in a container is controlled by the LayoutManager (already mentioned). Depends on the concrete implementation how fine-grained that's configurable

Upvotes: 6

mKorbel
mKorbel

Reputation: 109813

for create basic "Gap" between JComponents by using LayoutManagers Laying Out Components Within a Container

BorderLayout(int horizontalGap, int verticalGap)

GridLayout(int rows, int cols, int hgap, int vgap)

for most complex GUI you have to

1/ multiplayed JPanels (with different LayoutManager for each of JPanel too)

2/ by using How to Use GridBagLayout (by multiplayed JPanels with different LayoutManegars for each JPanel ...)

3/ use some Custom LayoutManager

Upvotes: 0

Hons
Hons

Reputation: 4046

That depends highly on the kind of Panel you are using. If this refers to swing you could put a Gridbaglayout on it and specify Insets like in this example

Upvotes: 0

Heisenbug
Heisenbug

Reputation: 39174

Use a BorderLayout for your JPanel.

Upvotes: 0

Related Questions