joshreesjones
joshreesjones

Reputation: 1954

How can I set the margin of a JLabel?

I have a JLabel that I want to add a margin to. It looks like this:

enter image description here

I read about setting an empty border with a certain thickness, but this would replace the current border. How can I add this margin?

Upvotes: 15

Views: 24326

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209052

"I read about setting an empty border with a certain thickness, but this would replace the current border. How can I add this margin?"

See CompoundBorder

A composite Border class used to compose two Border objects into a single border by nesting an inside Border object within the insets of an outside Border object. For example, this class may be used to add blank margin space to a component with an existing decorative border:

Border border = comp.getBorder();
Border margin = new EmptyBorder(10,10,10,10);
comp.setBorder(new CompoundBorder(border, margin));

Also see EmptyBorder

Upvotes: 25

Related Questions