Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the margin of a JLabel?

Tags:

java

swing

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?

like image 359
joshreesjones Avatar asked Mar 13 '14 16:03

joshreesjones


People also ask

How do you set a JLabel size?

You can set a fixed the size by setting the minimum, preferred and maximum size: setMinimumSize(width, height); setPreferredSize(width, height); setMaximumSize(width, height); As the Link from MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.

How do you add margins in Java?

We can set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.


1 Answers

"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

like image 146
Paul Samsotha Avatar answered Sep 20 '22 16:09

Paul Samsotha