| Constructor | Description |
|---|---|
JDialog() |
Creates an unowned, non-modal dialog without a title. |
JDialog(Frame owner) |
Creates a non-modal dialog with the specified frame as its owner. |
JDialog(Frame owner, boolean modal) |
Creates a dialog with the specified frame as its owner and modality. |
JDialog(Frame owner, String title) |
Creates a non-modal dialog with the specified frame as its owner and the specified title. |
JDialog(Frame owner, String title, boolean modal) |
Creates a dialog with the specified frame as its owner, the specified title, and modality. |
package jdialog;
import javax.swing.*;
public class SimpleJDialogExample {
private static void createAndShowGUI() {
// Create a simple JDialog
JDialog dialog = new JDialog();
dialog.setTitle("Simple Dialog");
dialog.setSize(200, 150);
dialog.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}

package jdialog;
import javax.swing.*;
public class JDialogOnFrame {
private static void createAndShowGUI() {
JFrame frame = new JFrame("Simple JDialog Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
// Create a simple JDialog
JDialog dialog = new JDialog(frame, "Simple Dialog", true);
dialog.setSize(200, 150);
// Center the dialog relative to the frame
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}

In this example:
JFrame is created and made visible.JDialog is created with a title, set to be modal, and given a size.setLocationRelativeTo(frame).setVisible(true).
| Method | Description |
|---|---|
add(Component comp) |
Adds the specified component to the container. |
dispose() |
Releases all of the native screen resources used by this window, its subcomponents, and all of its owned children. |
getDefaultCloseOperation() |
Returns the operation that occurs when the user initiates a “close” on this dialog. |
getTitle() |
Returns the title of the dialog. |
setDefaultCloseOperation(int operation) |
Sets the operation that will happen by default when the user initiates a “close” on this dialog. |
setTitle(String title) |
Sets the title for this dialog. |
setVisible(boolean b) |
Shows or hides this dialog depending on the value of parameter b. |
isModal() |
Returns whether this dialog is modal. |
setModal(boolean modal) |
Sets whether the dialog is modal. |
pack() |
Causes this window to be sized to fit the preferred size and layouts of its subcomponents. |
You must be logged in to submit a review.