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).

import java.awt.*;
import java.awt.event.*;
public class UPValidation extends Frame implements ActionListener
{
        DialogTest dt;
        static TextField tf1;
        public UPValidation()
        {
              Button btn = new Button("Password");
              tf1 = new TextField(10);
              btn.addActionListener(this);
              add(btn, "North");
              add(tf1, "South");
              tf1.setBackground(Color.pink);

              dt = new DialogTest(this);

              setSize(225,225);
              setVisible(true);
       }
       public void actionPerformed(ActionEvent e)
       {
             dt.showPlease();
       }
       public static void confirm(String str1)
       {
              if(str1.equals("jyostna"))
                          tf1.setText("Valid Please");
              else
                         tf1.setText("Sorry, Invalid Please");
       }
       public static void main(String args[ ])
       {
            new UPValidation();
       }
}
class DialogTest extends Dialog implements ActionListener
{
        TextField tf2;
        UPValidation upv;
         public DialogTest(Frame f1)
        {
             super(f1, "Dialog Box by Jyostna", true);
             setLayout(new FlowLayout());
             Button btn1 = new Button("Check Please");
             tf2 = new TextField(10);
             btn1.addActionListener(this);
             add(new Label("Enter Your Password"));
             add(tf2);
             add(btn1);

             setSize(225,125);
        }
        public void actionPerformed(ActionEvent e)
        {
             UPValidation.confirm(tf2.getText());
             setVisible(false);
         }
         public void showPlease()
         {
             setVisible(true);
         }
}

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 Comment

Your email address will not be published.