I want to leave the default border on my JButton
s, but put empty space around them as well. I'm using a vertical BoxLayout
.
I originally said nothing about the borders, and got single pixel LineBorder
s, which I want, but the buttons all butted up against each other.
I then tried button[i].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5))
. Rather than adding blank space around the button, it made the buttons' areas expand. It also removed the LineBorder
.
I then tried: button[i].setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5), button.getBorder()))
This gave me back the LineBorder
, but rather than adding blank space outside the line, it just extended the buttons' areas beyond the line!
I realise I can add blank boxes to space my buttons out, but I want space on the sides of them as well, which is why I want to add an EmptyBorder
. I'm new to Swing, so maybe there's an entirely better way of doing this that I don't know about :)
I'm using Jython, but the API should be the same as from Java.
Conceptually, the "empty borders" you want to add are not really part of the button (for example, they should not be clickable).
This is actually a matter of layout, so you should probably check the documentation of the layout manager you are using. For example:
hgap
and vgap
properties to specify the horizontal and vertical gaps between components.insets
of a GridBagConstraints object.And so on.
I think it's simpler to add the button to a panel and set the empty border to the panel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With