2

I know there is a way to extend a JLabel to paint 3D borders and a way to paint round borders, but how do you get both? Here is my code

protected void paintComponent(Graphics g) {
     g.setColor(getBackground());
     g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 25, 25);
     g.fill3DRect(10, 10, 30, 30, true);
     super.paintComponent(g);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bashir Beikzadeh
  • 761
  • 8
  • 15
  • 1
    does your code not work? what does it show? – Jakob Weisblat Jun 29 '12 at 09:35
  • You will have more success with `JLabel.setBorder()` as shown in an answer below than with overriding `paintComponent()`, because if you use `setBorder()` then the panel may adjust its size in its container. – Enwired Jun 29 '12 at 18:02

2 Answers2

3

Use LineBorder with rounded corners or a variant of the TextBubbleBorder.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

You refer this code to create Round Corner JLabel:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class RoundedLineBorder extends JPanel {

    public RoundedLineBorder() {
        super(true);
    setLayout(new BorderLayout());

        JLabel label = new JLabel("Rounded Corners");

        label.setHorizontalAlignment(JLabel.CENTER);

    LineBorder line = new LineBorder(Color.blue, 2, true);

        label.setBorder(line);

        add(label, BorderLayout.CENTER);
    }

    public static void main(String s[]) {
         JFrame frame = new JFrame("Rounded Line Border");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(500, 200);
         frame.setContentPane(new RoundedLineBorder());
         frame.setVisible(true);
    }
}
Vinesh
  • 933
  • 2
  • 7
  • 22