0

How can I customize a JDialog with round corners, no title, and no control buttons. Like the one below.

Desired JDialog

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
o.o
  • 19
  • 2
  • *but the dialog can not drag move as normal dialog. how to do ?* - see: [Moving Windows](https://tips4java.wordpress.com/2009/06/14/moving-windows/) – camickr Sep 29 '21 at 14:14

1 Answers1

7

As with most things in life, this is an illusion.

You're actually asking three questions...

  1. How to remove a windows decorations
  2. How to make a window transparent
  3. How to draw a "rounded" rectangle

All of things are relatively easy to do, knowing you need to them, that's another issue.

So, for the first two...

JDialog dialog = new JDialog();
dialog.setUndecorated(true);
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setModal(true);
dialog.setSize(640, 480);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);

You need to remove the window decorations and set the background to a transparent color. If you run the above code, you will see, nothing, kind of spooky actually. That's because the window has not decorations and is transparent.

The next step will require a custom component. Again, this is an illusion. We're going to make the component transparent, BUT, we're going to manually fill it.

public class RoundedPane extends JPanel {

    private int radius = 20;
    
    public RoundedPane() {
        setOpaque(false);
        setBorder(new EmptyBorder(10, 10, 10, 10));
        setLayout(new BorderLayout());
    }

    public void setRadius(int radius) {
        this.radius = radius;
        setBorder(new EmptyBorder(radius / 2, radius / 2, radius / 2, radius / 2));
        revalidate();
        repaint();
    }

    public int getRadius() {
        return radius;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(getBackground());
        g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
        g2.setColor(getForeground());
        g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
        super.paintComponent(g);
    }
}

So, simply put, this will create a JPanel, make it transparent (setOpaque(false)) and then override it's paintComponent so we can paint our rounded effect.

Now, if you put it altogether, you'll end up with something looking like this...

Of so rounded

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JDialog dialog = new JDialog();
                dialog.setUndecorated(true);
                dialog.setBackground(new Color(0, 0, 0, 0));
                dialog.setModal(true);
                dialog.setContentPane(new RoundedPane());
                dialog.setSize(640, 480);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        });
    }

    public class RoundedPane extends JPanel {

        private int radius = 20;

        public RoundedPane() {
            setOpaque(false);
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setLayout(new BorderLayout());
        }

        public void setRadius(int radius) {
            this.radius = radius;
            setBorder(new EmptyBorder(radius / 2, radius / 2, radius / 2, radius / 2));
            revalidate();
            repaint();
        }

        public int getRadius() {
            return radius;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(getBackground());
            g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
            g2.setColor(getForeground());
            g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
            super.paintComponent(g);
        }
    }
}

So, some immediate notes about the above example.

  • I've used setSize for this example deliberately. The RoundedPane should defer calculating the required size based on the Border and it's content, so we don't want to modify it, but since it will gave a preferred size of 0x0, I set the size of the dialog myself. You should rely on pack to get a better effect.
  • I've set RoundedPane to be dialog's contentPane. This will allow you to continue to add content directly to the dialog like you normally would. You can add content directly to the RoundedPane, but this also ensures that the existing contentPane doesn't interfere with our work.

You should also have a look at:

in this way i found this undecorated all the font type change ,how can i set font type in whole dialog.

The font is managed by the installed look and feel. If you do have issues with the font then either, you've done something wrong it's a platform specific issue (ie a bug)

All your font

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • but the dialog can not drag move as normal dialog. how to do ? – o.o Sep 29 '21 at 07:15
  • 1
    If you remove the functionality of the window decorations, you need to take over those responsibilities - [for example](https://stackoverflow.com/questions/16869877/how-to-remove-window-box-from-any-java-gui/16869893#16869893) – MadProgrammer Sep 29 '21 at 07:29
  • in this way i found this undecorated all the font type change ,how can i set font type in whole dialog. – o.o Oct 11 '21 at 09:19
  • The decoration of the frame should have no effect on the font style, as it's managed by the `UIManager` and the installed look and feel – MadProgrammer Oct 11 '21 at 09:21