Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java rounded corners on JFrame?

I have a login JFrame for my application and i would like to make the corners of the frame rounded by a few pixles.

Id like to do this without using transparency on the JFrame and having to use a background image inside a JPanel - is this possible?

like image 247
Alosyius Avatar asked Sep 21 '13 17:09

Alosyius


2 Answers

It's possible with undecorated frame, consider the following example:

JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setShape(new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50));
frame.setSize(300, 200);
frame.setVisible(true);

This code works on Java 7. For Java 6 (since update 10) you can do the same with AWTUtilities.setWindowShape:

JFrame frame = new JFrame();
frame.setUndecorated(true);
AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50));
frame.setSize(300, 200);
frame.setVisible(true);
like image 191
Jk1 Avatar answered Nov 02 '22 06:11

Jk1


Try this. It works :)

yourframe.setUndecorated(true);
yourframe.setBackground(new Color(0, 0, 0, 180));
yourframe.addComponentListener(new ComponentAdapter() {
               @Override
                public void componentResized(ComponentEvent e) {
                    setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 80, 80));
                }
            });
like image 45
Harsha Sampath Avatar answered Nov 02 '22 07:11

Harsha Sampath