43

I know how to create borders using BorderFactory but I don't see anything that lets me specify what sides I want the border on :S

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
Becky
  • 893
  • 3
  • 9
  • 14

4 Answers4

90

You can use the MatteBorder to specify the dimensions of the border in each side. The constructor of MatteBorder is:

public MatteBorder(int top,
                   int left,
                   int bottom,
                   int right,
                   Color matteColor)

So if you want to have a border only on the bottom and right sides of your JPanel, you could write something like this:

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, Color.BLACK));
IvanRF
  • 7,115
  • 5
  • 47
  • 71
Alex Ntousias
  • 8,962
  • 8
  • 39
  • 47
  • 8
    Is there an easy way to get the MatteBorder to use bevel style rounding for the corners? I need to use the matte border to control the sides of the element that are rendered, but i'm stuck with square corners. ;-( – emeraldjava Mar 23 '10 at 15:57
  • 3
    Plus 1 for actually providing code. This should be the accepted answer! – Hunter S Nov 10 '15 at 03:39
47

From Sun tutorial:

The next picture shows some matte borders. When creating a matte border, you specify how many pixels it occupies at the top, left, bottom, and right of a component.

(Java docs)

Community
  • 1
  • 1
Pool
  • 11,999
  • 14
  • 68
  • 78
6

Matte and empty border allow you to specify the sizes on each side, which may be zero.

The Border interface itself is quite easy to implement yourself if you want a custom look. I guess there may be third party libraries available containing styles not included within the Java library.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
2
text_field.setBorder( new MatteBorder(2, 0, 0, 0, Color.black));

The values can be varied accordingly.

Maksym
  • 2,650
  • 3
  • 32
  • 50
salman pk
  • 29
  • 1