Chris Smith
Chris Smith

Reputation: 3012

Java Swing border with no thickness

I have a bunch of JLabels in a GridBagLayout. I would like to highlight which one is "selected" by adding a line border around the "selected one". The problem with this is that the thickness of the border separates the JLabels apart and forms gaps in between them all. Is it possible to make the border with 0 thickness but still be visible?

Upvotes: 0

Views: 122

Answers (2)

camickr
camickr

Reputation: 324118

Is it possible to make the border with 0 thickness but still be visible?

You can use a CompoundBorder. Create a LineBorder as the outer and an EmptyBorder (with -1 offsets) as the inner.

The total size of the Border will be 0 and not take up any space, but it will paint on top of the label, instead of outside the bounds of the label.

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347204

Instead, set each "non-selected" JLabel with an EmptyBorder of the same pixel thickness as the LineBorder.

When selected, change the Border to a LineBorder and when unselected, change it to an EmptyBorder.

This will stop the layout from changing size...as it hasn't.

Also, a Border of 0 pixel thickness is...invisible...

Upvotes: 2

Related Questions