JPanel Padding in Java

Connor picture Connor · Mar 16, 2011 · Viewed 122.6k times · Source

I have a formatting question for my Java swing application. It should be fairly straightforward, but I am having difficulty finding any aid (Every topic seems to be regarding removing any default padding in JPanel). The text in my various JPanels hug the sides and top, touching the colored borders: how can I add padding? Thank you.

Answer

Julien Vermillard picture Julien Vermillard · Mar 16, 2011

Set an EmptyBorder around your JPanel.
Example:

JPanel p =new JPanel();
p.setBorder(new EmptyBorder(10, 10, 10, 10));