Java AWT Dialog Modal Modeless

Java AWT Dialog Modal Modeless


Dialog boxes are pop-up windows on the screen that appear for a small time to take either input or display output while a main application is running. The windows that appear for a brief time are known as transient windows. Dialog boxes are generally used to draw special attention of the user like displaying warnings.   Dialog box is a top-level window that comes with a border including a title bar. The dialog box can be made non-resizable and the default layout manager is BorderLayout which can be overridden.

Dialog basics

A dialog box works within a main program. It cannot be created as a standalone application. It is a child window and must be connected to a main program or to a parent window. Frame is a parent window as it can work independently. For example, the Find and Replace Dialog box cannot be obtained without opening MS-Word document. Likewise, File Deletion Confirmation box cannot appear without deleting a file.

Modal and Modeless

Two types of dialog boxes exist – modal and modeless. Modal dialog box does not allow the user to do any activity without dismissing (closing) it; example is File Deletion Confirmation dialog box. Modeless dialog box permits the user to do any activity without closing it; example is Find and Replace dialog box of MS Word. Java supports both styles of dialog boxes.

One more dialog box exists with AWT, known as FileDialog which gets the platform dependent file searching dialog box.

Following is the class signature

public class Dialog extends Window

Creating and Using Dialog Box

The following code involves two classes – one to create the main frame (UPValidation) and the other to create the dialog box (DialogTest ) to be attached to the main frame. That is, UPValidation class is a parent window and DialogTest is a child window. In dialog box program, it is necessary to attach or link the child window to the parent window. This is also a good example that illustrates linking of Java classes (earlier one is in "Java AWT List" illustration).

Java AWT Dialog Modal Modeless

The parent window, UPValidation.java, displays two components – a button (btn) and a text field (tf1). When the user clicks the button btn (with label Password), it calls the dialog box (by calling showPlease() method of DialogTest), a child window created by UPValidation. DialogTest window contains a text field tf2 and a button btn1 (of label Check). When the user enters a password in tf2 and clicks over the Check Please button, it calls the confirm() of UPValidation and passes the password entered by the user. Also it closes the dialog box as the validation is over by calling setVisible(false) . The confirm() method validates the password and displays the result in the text field tf1.

When the user clicks the Password button (btn), it calls the showPlease() method of DiologTest which displays the dialog box (by calling setVisible(true)).

super(f1, “Dialog Box by SNRao”, true);

To create a dialog box, the DialogTest class extends Dialog. In the above statement, f1 represents the frame of UPValidation. The second string parameter is the title of the dialog box. The third parameter true indicates a modal dialog box. The super() calls the super class Dialog constructor and passes the parameters. With the above parameters, the Dialog class constructs a dialog box and displays.

Leave a Reply

Your email address will not be published. Required fields are marked *