I have a JLabel
that I want to add a margin to. It looks like this:
I read about setting an empty border with a certain thickness, but this would replace the current border. How can I add this margin?
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.
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.
"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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With