user3733253
user3733253

Reputation: 53

JLabel remove space between text and border

I am creating a JLabel with only a text (customized from another part of the program). I cannot post images here, but the label comes with padding (meaning there is a gap between the text and the border.)

I don't want the padding to be so thick. Is there a way to compress the padding so it won't be so thick? These are the things I have tried (one by one, not altogether)

setPreferredSize(width, height);
setBorder(new EmptyBorder(new Insets(0,0,0,0)));
setBorder(new EmptyBorder(new Insets(-5,-5,-5,-5)));

but none of them works. at most they shrink my JLabel but the padding stays the same. The padding ends up covering my text instead. I understand setMargin may work but JLabel doesn't seem to have setMargin so I'm lost. Any help would be appreciated, thank you!

Upvotes: 1

Views: 3064

Answers (1)

user3733253
user3733253

Reputation: 53

I figured it out. The 'border' that I saw was actually the panel instead of just the label. The label's background itself was transparent (I did not setOpaque(true) for the label). Turns out my label has no padding at all, which is what I wanted. I understand why there is a padding on the panel too (done by FlowLayout) so all is well. Thank you mKorbel for the speedy response! I'll close the question.

Upvotes: 4

Related Questions