Reputation: 111
I'm building a Java Swing app and I'm trying to achieve a backgroundcolor inside the border,
for some reason it spills outside the border as you can see here:
Fyi I need the TitleBorder to remain -> "TitledBorder.TOP" and not "BELOW_TOP"
Build with IntelliJ IDEA with Java SDK 8
Thank you for your time
Upvotes: 0
Views: 617
Reputation: 324118
for some reason it spills outside the border
That is the way Swing painting works.
All Swing components have a parent / child relationship. So the top level component is painted, then the child is painted on top of the parent etc all the way down the parent/child tree. So each child overrides the background of its parent.
Read the section from the Swing tutorial on A Closer Look at the Painting Mechanism
If this is not acceptable to you then you would need to create a custom Border
that would first:
paint the outer area of the Border in the same color as the background of the parent component.
Then draw the TitleBorder
.
Or, maybe you could somehow do this by using a CompoundBorder
.
Read the section from the Swing tutorial on How to Use Borders for more information.
Upvotes: 1