Introduction
A dialog box, like a frame, is a pop-up window on which user interface components can be laid out. Typically, they are used to communicate with users and solicit information from them. Though a dialog resembles and behaves like a frame, there are some major differences between the two. They are:
- Unlike frames, a dialog has a parent (or an owner) window. When the parent window is closed, the dialogue closes automatically as well.
- The dialog box usually contains a number of UI components suitably arranged (laid out). Letting the user adjust the size of the dialog may affect the layout of the components in the dialog.
- A dialog box can be made modal. A modal dialog prevents user inputs to other windows in the application until the dialog is closed.
Dialog Constructors
- Dialog(Frame, boolean): Creates a brand-new dialogue that is initially invisible. The Frame acts as the parent of the dialog. When the parent dialogue closes, the dialogue also does. Whether or not the dialogue is modal is determined by the boolean value.
- Dialog(Frame, String, boolean): Creates a new dialog with the specified String as the title. The other two parameters are the same as in the previous constructor.
Note. Both Dialog constructors require a Frame as a parameter. This frame window acts as the parent of the dialog.
The dialog, like a frame, can act as a container. The layout of the dialog can be set using the setLayout() method, and components can be added to it using the add() method.
Methods used
- setResizable(boolean): Sets the resizable flag of the dialog. If the boolean value passed is true, the dialog is made resizable.
- isModal(): Returns true if the dialog is modal. A modal dialog prevents the user input to other windows in the application until the dialog is closed.
- isResizable(): Returns true if the dialog is resizable.
Create a MessageBox dialog class that displays a Message in a dialog window.
import java.awt.*;
import java.awt.event.*;
public class MessageBox extends Dialog {
MessageBox(Frame fm, String lab) {
super(fm, "Message", true);
setLayout(new GridLayout(2, 1, 0, 0));
Panel p1 = new Panel();
Panel p2 = new Panel();
Button b1, b2;
p1.setFont(new Font("TimesRoman", Font.BOLD, 18));
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 15));
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
p1.add(new Label(lab));
b1 = new Button("Ok");
b1.addActionListener(new B1());
p2.add(b1);
b2 = new Button("Cancel");
b2.addActionListener(new B1());
p2.add(b2);
add(p1);
add(p2);
setSize(350, 125);
setTitle("Message Box");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
System.exit(0);
}
});
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
Button Ok = (Button) e.getSource();
String s = Ok.getLabel();
if (s.equals("Ok") || s.equals("Cancel")) {
dispose();
System.exit(0);
}
} catch (Exception n) {
}
}
}
}
Create a separate file for the source code and save it as MessageApplication.java.
import java.awt.*;
public class MessageApplication extends Frame {
boolean a;
MessageApplication() {
MessageBox mb = new MessageBox(this, "Java Alert! This is Ashish Bhatnagar Message Box");
mb.setLocation(200, 200);
mb.setVisible(true);
a = false;
}
public static void main(String args[]) {
MessageApplication fm = new MessageApplication();
System.out.println("Popping Out Message Box");
fm.setVisible(true);
}
}


Join the conversation! Your thoughts help the community grow.