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

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

As with most things in life, this is an illusion.
You're actually asking three questions...
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...
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.
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.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:
JFrame JavaDocsin 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)