1

Is there a way, if so how, to add a border to a button but only to the bottom of it? I want to change the colour of the bottom border of the button. Is it possible?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3650137
  • 31
  • 1
  • 6
  • 3
    Have you looked at [MatteBorder](https://docs.oracle.com/javase/7/docs/api/javax/swing/border/MatteBorder.html)? It could be as simple as button.setBorder(new MatteBorder(0, 0, 2, 0, Color.RED)); – mprabhat Nov 28 '14 at 21:01
  • No sorry, though I have searched it up. I am not familiar with the syntax. From what I have seen and understand, it is mostly related to icons etc. How would I implement a MattBorder to a JButton? would it be something similar to the following: Border solidBorder = new MatteBorder(10, 5, 2, 20, Color.RED) - but how do I assign it to a specific button? – user3650137 Nov 28 '14 at 21:07
  • Sorry, I have just seen your edit, Ill try it now. Thank you. – user3650137 Nov 28 '14 at 21:08
  • Please post your code as Answer and then select it as correct answer. It will help others in future – mprabhat Nov 28 '14 at 21:11
  • I concur with @mprabhat; please don't hesitate to reference my answer in yours. – trashgod Nov 29 '14 at 17:12

1 Answers1

2

A button's appearance is controlled by the UI delegate of the user's chosen Look & Feel. Borders applied directly to the button don't always appear. As suggested by the authors of setBorder(), add the border proposed by @mprabhat to the button's surrounding container. Starting from this example, I made the following change to the ButtonPanel constructor:

public ButtonPanel(int i) {
    this.setBackground(new Color(rnd.nextInt()));
    this.setBorder(new MatteBorder(0, 0, 2, 0, Color.RED));
    this.add(new JButton("Button " + String.valueOf(i)));
}

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045