Introducing padding in a JLabel

22
Aug/09
0

To introduce padding in a JLabel can we use an EmptyBorder, where the attribute width will be our padding. Like this:

...
JLabel jLabel = new JLabel("My JLabel");
//Border used as padding
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);

jLabel.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
...

Here, the JLabel contains a padding with 10 pixels in top, right, bottom and left respectively.

0

If you want to put border around the JLabel, can use a CompoundBorder, inserting Border and EmptyBorder (padding). Like this:

...
JLabel jLabel = new JLabel("Meu JLabel");
//Border used as padding
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
//JLabel will be involved for this border
Border border = BorderFactory.createLineBorder(Color.BLUE);

jLabel.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
...

1

Download the source code of this sample here.

You liked it? Then, please, make a comment! Campaign: "Comment doesn't make your fingers falls".