Java AWT (Abstract Window Toolkit) provides a class called Dialog that represents a top-level window containing a message and/or some options for the user to choose from. A Dialog can be modal or non-modal, depending on whether it blocks user input to other windows.
Here is an example of how to create and display a simple Dialog in Java AWT:
import java.awt.*; import java.awt.event.*; public class MyDialog extends Dialog implements ActionListener { public MyDialog(Frame parent) { super(parent, "My Dialog", true); // true to make it modal setLayout(new FlowLayout()); add(new Label("Hello from Dialog!")); Button okButton = new Button("OK"); okButton.addActionListener(this); add(okButton); setSize(200, 100); } public void actionPerformed(ActionEvent e) { setVisible(false); } public static void main(String[] args) { Frame f = new Frame("My Frame"); f.setSize(300, 200); f.setVisible(true); MyDialog d = new MyDialog(f); d.setVisible(true); } }
This code creates a subclass of Dialog called MyDialog
and sets its layout to FlowLayout
. It adds a Label
with the text “Hello from Dialog!” and a Button
with the label “OK”. When the button is clicked, the setVisible
method is called with false
as its argument, which hides the Dialog.
In the main
method, a Frame
is created and made visible, and an instance of MyDialog
is created and made visible as well. Because we passed true
as the third argument to the Dialog
constructor, the Dialog is modal, which means the user cannot interact with the Frame while the Dialog is open.
Note that Java AWT has been replaced by Swing, which provides more powerful and flexible GUI components. However, Dialogs can still be useful in some situations.
AWT Dialog class declaration:
The Dialog
class in Java AWT is declared as follows:
public class Dialog extends Window { // Fields public static final ModalityType DEFAULT_MODALITY_TYPE; public static final int MODELESS; public static final int DOCUMENT_MODAL; public static final int APPLICATION_MODAL; // Constructors public Dialog(Frame owner); public Dialog(Frame owner, String title); public Dialog(Frame owner, String title, boolean modal); public Dialog(Dialog owner); public Dialog(Dialog owner, String title); public Dialog(Dialog owner, String title, boolean modal); // Methods public void addNotify(); public void setTitle(String title); public void setResizable(boolean resizable); public void setUndecorated(boolean undecorated); public void setModal(boolean modal); public void setModalityType(ModalityType type); public void setDefaultCloseOperation(int operation); public void pack(); public void setLocationRelativeTo(Component c); public void setVisible(boolean b); public void dispose(); }
The Dialog
class extends the Window
class and provides methods for creating and managing dialog boxes. It has several constructors, including ones that take a Frame
or a Dialog
as the owner of the dialog, and one that allows you to specify whether the dialog should be modal or not.
The Dialog
class also provides several methods for setting and getting properties of the dialog box, such as its title, modality type, and whether it is resizable or not. Additionally, it provides methods for displaying and hiding the dialog box, as well as for disposing of it when it is no longer needed.
Java AWT Dialog Example:
Here’s an example of how to create and display a simple Dialog in Java AWT:
import java.awt.*; import java.awt.event.*; public class MyDialog extends Dialog implements ActionListener { public MyDialog(Frame parent) { super(parent, "My Dialog", true); // true to make it modal setLayout(new FlowLayout()); add(new Label("Hello from Dialog!")); Button okButton = new Button("OK"); okButton.addActionListener(this); add(okButton); setSize(200, 100); } public void actionPerformed(ActionEvent e) { setVisible(false); } public static void main(String[] args) { Frame f = new Frame("My Frame"); f.setSize(300, 200); f.setVisible(true); MyDialog d = new MyDialog(f); d.setVisible(true); } }
In this example, we create a subclass of Dialog
called MyDialog
. We pass a Frame
instance to the Dialog
constructor, indicating that the Frame
is the owner of the dialog. We also set the title of the dialog to “My Dialog” and make it modal by passing true
as the third argument to the constructor.
We then set the layout of the dialog to FlowLayout
and add a Label
with the text “Hello from Dialog!” and a Button
with the label “OK”. We add an ActionListener
to the Button
that calls setVisible(false)
when the button is clicked, which hides the dialog.
In the main
method, we create a Frame
and make it visible. We then create an instance of MyDialog
and make it visible as well. When the dialog is displayed, the user cannot interact with the Frame
until they close the dialog by clicking the “OK” button.