Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded Border on one side only java

Tags:

java

border

swing

I need to create a rounded border on one side of a component only.

This code creates a rounded border:

  new LineBorder(Color.RED, 3, true)

I have seen this thread which shows you how to create a matte border that can be used on one side of a component only, however a matte border isn't rounded.

Is it possible to have a rounded border on one side only?

Edit:

I have tried using compound border like this:

    cell.setBorder(BorderFactory.createCompoundBorder(
        new LineBorder(borderColor, 3, true),
        BorderFactory.createMatteBorder(0, 3, 0, 0, Color.black)));

But it doesn't work...

like image 993
David Avatar asked Aug 12 '11 13:08

David


People also ask

Which CSS properties can you use to create a rounded corner on just the top left and top right corners of an element?

With the CSS border-radius property, you can give any element "rounded corners".

How do you give border radius only on top?

CSS Syntaxborder-top-left-radius: length|% [length|%]|initial|inherit; Note: If you set two values, the first one is for the top border, and the second one for the left border. If the second value is omitted, it is copied from the first. If either length is zero, the corner is square, not rounded.

How do you remove a border radius?

Removing Border RadiusUse the . rounded-0 helper class to remove all of an elements radius or select by side or corner; e.g. . rounded-l-0 and . rounded-tr-0 .


2 Answers

You can override the method of LineBorder and draw all you need there From sources of LineBorder

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Color oldColor = g.getColor();
        int i;

    /// PENDING(klobad) How/should do we support Roundtangles?
        g.setColor(lineColor);
        for(i = 0; i < thickness; i++)  {
        if(!roundedCorners)
                g.drawRect(x+i, y+i, width-i-i-1, height-i-i-1);
        else
SET CLIP HERE TO DRAW ONLY NECESSARY PART
                g.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, thickness, thickness);
        }
        g.setColor(oldColor);
    }
like image 169
StanislavL Avatar answered Oct 27 '22 22:10

StanislavL


LineBorder only supports all corners rounded or not. A compound border (as the JavaDoc states) uses one border for the outside and one for the inside and thus does not distinguish between left/right or top/bottom.

I'm afraid you'd either have to write your own Border implementation or look for one that's already made by someone else (external library).

like image 22
Thomas Avatar answered Oct 27 '22 22:10

Thomas