1. AWT Dialog (Top-Level Window)
Java AWT Dialog is a top-level window like AWT Frame which we used many times in our examples. In AWT, the Dialogs which come with a title and border will collect input from the users. The default layout for the AWT Dialog is a BorderLayout and one can change it using the setLayout method. A dialog can have another top-level window as its parent. It can be a Frame Window or even one more Dialog. This Parent-Child relation will help in closing, minimizing, or restoring the dialog when its parent undergoes with the same change. For example, if we close a parent, then AWT will close all the child Dialogs.
2. AWT Dialog Constructor Params
We can construct Java AWT Dialog with three parameters. Have a look at the below picture:

Here, the first Param tells the Dialog Title. The second param is to mention the parent of this dialog. In this example, we are going to use AWT Frame as its parent. But it can be any top-level window. The third param tells how AWT need to display the dialog. This boolean param can display the dialog as either Modal or as Modeless. We will learn the difference between them in the next section.
3. Modal & Modeless Dialogs
Once a dialog is constructed, we can launch it as Modal or Modeless. Let us understand how the Modal and Modeless dialogs behave. Have a look at the below picture:
In the first part of the depiction, marker 1 is the calling code. After launching the dialog as Modal, the execution control is transferred to Maker 2, which is the belonging to the dialog. The code marked by 3 is from the calling code, which falls after the call to the Modal dialog. Execution of the code marked 3 is blocked till user closes the dialog. We can close the AWT dialog either by clocking OK button and by clicking the cancel button. The red arrow shows the break on execution and how it resumes after the dialog returns the control to its caller. In this example, we are going to create a modal dialog.
The second part of the picture shows the same dialog when called as modeless. There is no blockage of code execution in caller perspective. Means, the code marked as 5 and 6 are executed at the same time. The calling code (Marked as 4) will continue with the execution (Marked as 6) and it will not wait for the dialog to be closed.
4. About this AWT Modal Dialog Example
As already told, in this example, we will create an AWT Dialog and display that a modal dialog to collect input from the user. An AWT Frame will launch the dialog to collect the input. Have a look at the below picture:
In this example, we create AWT Frame which shows personal data. Here, fields Name and Age collect user input from the Frame itself. Address multi-line text field is read-only and ellipsis button next to it will launch a dialog to gather user input. The frame wants to display the address in a precise format and hence it invokes the dialog.
Once the dialog is displayed as modal, the code execution in the Frame window is blocked. This means, the user can not interact with any of the UI components in the Frame Window before closing the dialog. In the dialog, the user can fill the details required for the address field and click OK. Or they can also click Cancel and that is not a usual workflow.
After filling the data, the user clicks the OK button and dialog hides. The frame window read the address information from the dialog and displays that in address multi-line field. Note, the frame gets the address in a formatted way from the dialog. Since, address field is disabled, the user can not edit its content manually. They can feed the address only via the dialog and there by the frame gets the formatted address.
5. Prepare & Display AWT Frame
In this section, we will define the frame window with the required controls. This frame will later display the AWT Dialog.
5.1 Address Member Variable
The user is not allowed to edit the multi-line text in the frame window. The code edits it, of course, after closing the dialog. So, we declare a AWT TextArea
as a class member variable.
1 2 |
//sample 01: Declaration TextArea taAddress; |
5.2 Components Required for The Frame
Next, we create AWT components required for the Frame window. At line 8, we create the AWT TextArea
component, and we also disable it by calling the method setEnabled
by sending a boolean false. The ellipsis button at line 7 will launch the AWT dialog to fill the TextArea
with the address data. After creating these controls, using the add
method of the AWT Frame, we add them to it. Note, in our example, FlowLayout
manager manages these controls. Also note the order in which we add the components to the AWT Frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample 02: Add a Button, Label and Text Area Label lblAddress = new Label("Address"); Label lblName = new Label("Name"); Label lblAge = new Label("Age"); TextField txtName = new TextField(30); TextField txtAge = new TextField(3); Button btnAddress = new Button("..."); taAddress = new TextArea(3,40); taAddress.setEnabled(false); //Sample 03: Add Controls to Frame Window setLayout(new FlowLayout()); add(lblName); add(txtName); add(lblAge); add(txtAge); add(lblAddress); add(taAddress); add(btnAddress); |
5.3 Register Ellipsis Button with ActionListener
Since the ellipsis button needs to respond to the user by displaying the AWT Dialog, we need to handle the ActionEvent
and hence our Frame class implements the ActionListener
.
1 2 3 4 |
//Sample 04: Add Action Listener public class FrameWin extends Frame implements WindowListener, ActionListener { |
We then register the ellipsis button with the AWT ActionListener. Since the dialog is not ready yet, we will postpone the handler function for now and start writing code for the AWT Dialog.
1 2 |
//Sample 05: Register the Button with Listener btnAddress.addActionListener(this); |
6. Preparing Components of AWT Dialog
6.1 AddressDlg Text Fields
We extend our dialog AddressDlg
from the
AWT Dialog and define five AWT TextField
components as its members. The string member AddrOut
will have the formatted address information.
1 2 3 4 5 6 7 8 9 |
public class AddressDlg extends Dialog { //Sample 06: Class Members TextField txtFlat; TextField txtStreet; TextField txtCity; TextField txtState; TextField txtPin; private String AddrOut; |
6.2 Construct AddressDlg
The constructor of our address dialog gets three params, and it hands over those to the base class constructor. We already looked at the dialog constructor in section 2 of this example.
1 2 3 4 5 |
//Sample 07: Constructor public AddressDlg(Frame parent, String title, boolean isModal) { super(parent, title, isModal); } |
6.3 Prepare AddressDlg AWT Dialog Components
Here, we write a method InitDialog
, which we call later from the Constructor. After setting the size and position, we set Flow Layout for our dialog. Then, the method InitDialog creates all the components required to get user input to form the formatted Address information. You can also see, we create OK and Cancel button and set action commands to them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
//Sample 08: Arrange the Control private void InitDialog() { //Sample 8.1 Set Size and Location setSize(380, 200); setLocation(300,200); setLayout(new FlowLayout()); //Sample 8.2: Create Labels Label lblFlatNo = new Label("House/Flat :"); Label lblStreet = new Label("Street :"); Label lblCity = new Label("City :"); Label lblState = new Label("State :"); Label lblZip = new Label("Zip :"); //Sample 8.3: Create Text Boxes txtFlat = new TextField(30); txtStreet = new TextField(30); txtCity = new TextField(30); txtState = new TextField(30); txtPin = new TextField(10); //Sample 8.4: Create Buttons Button BtnOK = new Button("OK"); Button BtnCancel = new Button("Cancel"); BtnOK.setActionCommand("OK"); BtnCancel.setActionCommand("Cancel"); //sample 8.5: Add the Controls add(lblFlatNo); add(txtFlat); add(lblStreet); add(txtStreet); add(lblCity); add(txtCity); add(lblState); add(txtState); add(lblZip); add(txtPin); add(BtnOK); add(BtnCancel); |
The user will fill the text fields in the dialog and then click the OK button. There is also a possibility the user can cancel the dialog by clicking the Cancel button or ‘X’ button of the dialog. We will soon handle the button events in the coming sections. But, to respond to dialog’s ‘X’ button click, we need to implement WindowListener
.
1 2 3 |
//Sample 09: Implement Window Listener public class AddressDlg extends Dialog implements WindowListener |
Now, from the InitDialog
method, we can tie the dialog with the Window Listener. Below, we make a call to the addWindowListener
method and pass ‘this’
object stating the listener is dialog itself.
1 2 |
//Sample 10: Register with Window Listener addWindowListener(this); |
To respond to the ‘X’ button click, we handle windowClosing
method. In this method, the
setVisible method hides the dialog as it takes in the false as a param value. As you guessed right, we will use this same method while launching the dialog with a boolean true as param value. Note, we handled only windowClosing
and provided a dummy code for other handler functions.
1 2 3 4 5 6 7 8 9 10 |
public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { //Sample 11: Hide the Dialog this.setVisible(false); } |
Finally, we call the InitDialog method from the AddressDlg Ctor. This will create all the components needed for the dialog and FlowLayout manager will take care of laying out them.
1 2 |
//Sample 12: Initialize the Dialog InitDialog(); |
7. Launch Dialog as Modal
In section 5.3, we registered the ellipsis button of the Frame with the ActionListener
. Now, we will implement the actionPerformed
handler and it will launch the AddressDlg AWT Dialog.
1 2 3 4 5 |
public void actionPerformed(ActionEvent e) { //Sample 13: Launch the Dialog AddressDlg addrDlg = new AddressDlg(this, "Address Form", true); addrDlg.setVisible(true); } |
In the above handler, we create the instance of our AWT Dialog
derived AddressDlg
instance. The first param is the Frame instance, and it shows our Frame window is the owner of the AddressDlg. So, if user closes the Frame Window, the AddressDlg will be destroyed. The second param is the title for the dialog and the third one tells we want to display the dialog as Modal Dialog. Once address dialog is launched via
setVisible method call, AWT will not allow the user to interact with the Frame Window. Once the AddressDlg is closed, the Frame Window will receive the focus. Now you can check launching the dialog by clicking the ellipsis button. Our next task is to read the user edited input from the AWT Dialog.
8. Handle AWT Dialog OK & Cancel Buttons
In this section, we will handle the OK and Cancel button click events. To handle these events, our Dialog class should implement the ActionListener
. Note, we already made our frame window as a WindowListener
. Below is the code:
1 2 3 4 5 |
//Sample 09: Implement Window Listener //Sample 14: Implement Action Listener public class AddressDlg extends Dialog implements ActionListener, WindowListener { |
Both the buttons in our dialog are signing with the ActionListener
by calling the method addActionListener
. After this method, whenever user clicks the buttons, Java AWT will send the ActionEvent
to the handler functions which we will write next and below is the code for registering the button with the ActionListeners.
1 2 3 |
//Sample 14.1: Register Action Listener BtnOK.addActionListener(this); BtnCancel.addActionListener(this); |
Now, we will implement the handler function actionPerformed. Since both OK and Cancel button sends the ActionEvent to the same handler function, inside the handler we make use of the getActionCommand to know which one producing the event. When the action command is OK, we read all the text field values present in this dialog via getText method. Then, we do string concatenation and store the formatted address in the AddrOut field. Remember, this member is private, and our frame class will make use of a function to read this formatted address. When the user clicks the Cancel button, we set a value ‘None’ in the AddrOut field.
No matter, user pressed OK or Cancel, we hide our Dialog as a last step. By this time, we have the AddrOut contains either formatted address entered by the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Override public void actionPerformed(ActionEvent e) { //Sample 14.2 Fill the AddrOut Private Member String action = e.getActionCommand(); if (action.compareTo("OK") == 0) { AddrOut = txtFlat.getText() + "\r\n" + txtStreet.getText() + "\r\n" + txtCity.getText() + " - " + txtState.getText() + "\r\n" + txtPin.getText(); } else { AddrOut = "None"; } setVisible(false); } |
As already told, we write a public member function to return the formatted address stored in the private member variable AddrOut. The frame class which is launching the dialog will make a call to the getAddrOut and get the formatted address string.
1 2 3 4 |
//Sample 14.3 Fill the AddrOut Private Member public String getAddrOut() { return AddrOut; } |
9. Read Dialog Data
Remember, in the actionPerformed
handler of the Frame class, we launched the Dialog. In the actionPerformed handler of the dialog, we formatted the address and stored that in the internal member variable. Now have a look at the below code:
1 2 3 4 5 6 7 8 |
public void actionPerformed(ActionEvent e) { //Sample 13: Launch the Dialog AddressDlg addrDlg = new AddressDlg(this, "Address Form", true); addrDlg.setVisible(true); //Sample 15: Set the Formatted Text taAddress.setText(addrDlg.getAddrOut()); } |
At line 4, Java AWT blocks the code execution and launches the dialog. Once the user closes the dialog, the execution control resumes from line 7. Now, we make use of the public function getAddrOut
written in the dialog class to read the formatted address which was collected from the user. We then assigned the return value of the string to the TextArea by calling the setText
method.
10. AWT Dialog Explained – Youtube
11. Code Reference
11.1 MainEntryAwt.java
1 2 3 4 5 6 7 8 9 10 11 |
package AwtDemoPkg; import java.awt.Frame; public class MainEntryAwt { public static void main(String[] args) { //Sample 00: Make the FrameWindow Visible FrameWin fw = new FrameWin("AWT Dialog Example"); fw.setVisible(true); } } |
11.2 FrameWin.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 04: Add Action Listener public class FrameWin extends Frame implements WindowListener, ActionListener { //sample 01: Declaration TextArea taAddress; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(420, 160); setLocation(100,100); addWindowListener(this); //Sample 02: Add a Button, Label and Text Area Label lblAddress = new Label("Address"); Label lblName = new Label("Name"); Label lblAge = new Label("Age"); TextField txtName = new TextField(30); TextField txtAge = new TextField(3); Button btnAddress = new Button("..."); taAddress = new TextArea(3,40); taAddress.setEnabled(false); //Sample 03: Add Controls to Frame Window setLayout(new FlowLayout()); add(lblName); add(txtName); add(lblAge); add(txtAge); add(lblAddress); add(taAddress); add(btnAddress); //Sample 05: Register the Button with Listener btnAddress.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void actionPerformed(ActionEvent e) { //Sample 13: Launch the Dialog AddressDlg addrDlg = new AddressDlg(this, "Address Form", true); addrDlg.setVisible(true); //Sample 15: Set the Formatted Text taAddress.setText(addrDlg.getAddrOut()); } } |
11.3 AddressDlg.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import java.awt.Button; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 09: Implement Window Listener //Sample 14: Implement Action Listener public class AddressDlg extends Dialog implements ActionListener, WindowListener { //Sample 06: Class Members TextField txtFlat; TextField txtStreet; TextField txtCity; TextField txtState; TextField txtPin; private String AddrOut; //Sample 07: Constructor public AddressDlg(Frame parent, String title, boolean isModal) { super(parent, title, isModal); //Sample 12: Initialize the Dialog InitDialog(); } //Sample 08: Arrange the Control private void InitDialog() { //Sample 8.1 Set Size and Location setSize(380, 200); setLocation(300,200); setLayout(new FlowLayout()); //Sample 8.2: Create Labels Label lblFlatNo = new Label("House/Flat :"); Label lblStreet = new Label("Street :"); Label lblCity = new Label("City :"); Label lblState = new Label("State :"); Label lblZip = new Label("Zip :"); //Sample 8.3: Create Text Boxes txtFlat = new TextField(30); txtStreet = new TextField(30); txtCity = new TextField(30); txtState = new TextField(30); txtPin = new TextField(10); //Sample 8.4: Create Buttons Button BtnOK = new Button("OK"); Button BtnCancel = new Button("Cancel"); BtnOK.setActionCommand("OK"); BtnCancel.setActionCommand("Cancel"); //sample 8.5: Add the Controls add(lblFlatNo); add(txtFlat); add(lblStreet); add(txtStreet); add(lblCity); add(txtCity); add(lblState); add(txtState); add(lblZip); add(txtPin); add(BtnOK); add(BtnCancel); //Sample 10: Register with Window Listener addWindowListener(this); //Sample 14.1: Register Action Listener BtnOK.addActionListener(this); BtnCancel.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { //Sample 11: Hide the Dialog this.setVisible(false); } @Override public void actionPerformed(ActionEvent e) { //Sample 14.2 Fill the AddrOut Private Member String action = e.getActionCommand(); if (action.compareTo("OK") == 0) { AddrOut = txtFlat.getText() + "\r\n" + txtStreet.getText() + "\r\n" + txtCity.getText() + " - " + txtState.getText() + "\r\n" + txtPin.getText(); } else { AddrOut = "None"; } setVisible(false); } //Sample 14.3 Fill the AddrOut Private Member public String getAddrOut() { return AddrOut; } } |
Categories: AWT
Tags: AWT Dialog, AWT Frame, Modal, Modeless, setVisible