Bashir Beikzadeh
Bashir Beikzadeh

Reputation: 781

How to make 3D rounded corner JLabel in Java?

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);

Upvotes: 2

Views: 3812

Answers (2)

Vinesh
Vinesh

Reputation: 943

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);
    }
}

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

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

Upvotes: 3

Related Questions