Denis
Denis

Reputation: 503

Java Swing - JButton wrong border

I try to set new red border to my "Ok" button, but instead of this1 I get this2 from this this. How to make 1-st picture? (it's photoshop)

I try

button.setBorder(new LineBorder(Color.RED, 1));

Upvotes: 1

Views: 134

Answers (1)

alterfox
alterfox

Reputation: 1695

You overrided the button's default border. You should set a compound border using the existing one and a new red one.

Border innerBorder = button.getBorder();
Border outerBorder = new LineBorder(Color.RED, 1);
button.setBorder(new CompoundBorder(outerBorder, innerBorder));


Here is a one liner combined with using the BorderFactory :-)

button.setBorder(BorderFactory.createCompoundBorder(
    BorderFactory.createLineBorder(Color.RED, 1),
    button.getBorder()));


Using BorderFactory - http://docs.oracle.com/javase/tutorial/uiswing/components/border.html#createapi

Upvotes: 2

Related Questions