Tomerz
Tomerz

Reputation: 111

Java swing background color fill outside the border

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:

enter image description 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

Answers (1)

camickr
camickr

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

  1. So first the background of the panel is painted.
  2. then the border is painted on top of the panel. In the case of a TitleBorder only the text and line is painted on top of the background.

If this is not acceptable to you then you would need to create a custom Border that would first:

  1. paint the outer area of the Border in the same color as the background of the parent component.

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

Related Questions