Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java AWT Dialog Example

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:

AWT Dialog Constructor
AWT Dialog Constructor

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:

Java AWT Dialog as Modal and Modeless
Java AWT Dialog as Modal and Modeless

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:

About the AWT Dialog Example
About the AWT Dialog Example

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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:

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.

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.

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.

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:

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




Categories: AWT

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.