Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top