How to add a space around the edge of Jbutton?

I want to leave the default border on my JButtons, but also place blank space around them. I use vertical BoxLayout.

  • I initially did not say anything about borders and got one pixel LineBorders that I want, but all the buttons were connected to each other.

  • Then I tried button[i].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)). Instead of adding empty space around the button, it increased the area of โ€‹โ€‹the buttons. He also deleted LineBorder.

  • Then I tried: button[i].setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5), button.getBorder()))

This returned me LineBorder, but instead of adding white space outside the line, it just expanded the button areas outside the line!

I understand that I can add empty boxes to touch my buttons, but I also need space on the sides, so I want to add EmptyBorder. I'm new to Swing, so maybe there is a better way to do this that I don't know about :)

I am using Jython, but the API should be the same as Java.

+5
source share
2 answers

Conceptually, the โ€œempty bordersโ€ you want to add are not really part of the button (for example, they should not be interactive).

This is actually a layout issue, so you should probably check the documentation of the layout manager you are using. For instance:

.

+4

, .

+3

All Articles