AWT Dialog Class

Dialog control represents a top-level window with a title and a border used to take some form of input from the user.
Following is the declaration for java.awt.Dialog class:
public class Dialog
extends Window
Following are the fields for java.awt.Image class:
static Dialog.ModalityType DEFAULT_MODALITY_TYPE  -- Default modality type for modal dialogs.
static Dialog.ModalityType DEFAULT_MODALITY_TYPE  -- Default modality type for modal dialogs.
Dialog(Dialog owner)  
Constructs an initially invisible, modeless Dialog with the specified owner Dialog and an empty title.
Dialog(Dialog owner, String title)  
Constructs an initially invisible, modeless Dialog with the specified owner Dialog and title.
Dialog(Dialog owner, String title, boolean modal)  
Constructs an initially invisible Dialog with the specified owner Dialog, title, and modality.
Dialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) 
Constructs an initially invisible Dialog with the specified owner Dialog, title, modality and GraphicsConfiguration.
Dialog(Frame owner)  
Constructs an initially invisible, modeless Dialog with the specified owner Frame and an empty title.
Dialog(Frame owner, boolean modal)  
Constructs an initially invisible Dialog with the specified owner Frame and modality and an empty title.
Dialog(Frame owner, String title)  
Constructs an initially invisible, modeless Dialog with the specified owner Frame and title.
Dialog(Frame owner, String title, boolean modal)  
Constructs an initially invisible Dialog with the specified owner Frame, title and modality.
Dialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc)
Constructs an initially invisible Dialog with the specified owner Frame, title, modality, and GraphicsConfiguration.
Dialog(Window owner)  
Constructs an initially invisible, modeless Dialog with the specified owner Window and an empty title.
Dialog(Window owner, Dialog.ModalityType modalityType)  
Constructs an initially invisible Dialog with the specified owner Window and modality and an empty title.
Dialog(Window owner, String title)  
Constructs an initially invisible, modeless Dialog with the specified owner Window and title.
Dialog(Window owner, String title, Dialog.ModalityType modalityType)  
Constructs an initially invisible Dialog with the specified owner Window, title and modality.
Dialog(Window owner, String title, Dialog.ModalityType modalityType, GraphicsConfiguration gc)  
Constructs an initially invisible Dialog with the specified owner Window, title, modality and GraphicsConfiguration
void addNotify() 
Makes this Dialog displayable by connecting it to a native screen resource.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Dialog.
Dialog.ModalityType getModalityType() 
Returns the modality type of this dialog.
String	getTitle() 
Gets the title of the dialog.
void hide()
Deprecated. As of JDK version 1.5, replaced by setVisible(boolean).
boolean isModal() 
Indicates whether the dialog is modal.
boolean isResizable() 
Indicates whether this dialog is resizable by the user.
boolean isUndecorated() 
Indicates whether this dialog is undecorated.
protected  String paramString() 
Returns a string representing the state of this dialog.
void setModal(boolean modal) 
Specifies whether this dialog should be modal.
void setModalityType(Dialog.ModalityType type) 
Sets the modality type for this dialog.
void setResizable(boolean resizable) 
Sets whether this dialog is resizable by the user.
void setTitle(String title) 
Sets the title of the Dialog.
void setUndecorated(boolean undecorated) 
Disables or enables decorations for this dialog.
void setVisible(boolean b) 
Shows or hides this Dialog depending on the value of parameter b.
void show() 
Deprecated. As of JDK version 1.5, replaced by setVisible(boolean).
void toBack() 
If this Window is visible, sends this Window to the back and may cause it to lose focus or activation if it is the focused or active Window.
This class inherits methods from the following classes:
java.awt.Window
java.awt.Window
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showDialogDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showDialogDemo(){
      headerLabel.setText("Control in action: Dialog"); 
      Button showAboutDialogButton = new Button("Show About Dialog");
      showAboutDialogButton.addActionListener(new ActionListener() {
	     @Override
         public void actionPerformed(ActionEvent e) {
            AboutDialog aboutDialog = new AboutDialog(mainFrame);
            aboutDialog.setVisible(true);
         }
      });

      controlPanel.add(showAboutDialogButton);
      mainFrame.setVisible(true);  
   }

   class AboutDialog extends Dialog {
      public AboutDialog(Frame parent){
         super(parent, true);         
         setBackground(Color.gray);
         setLayout(new BorderLayout());
         Panel panel = new Panel();
         panel.add(new Button("Close"));
         add("South", panel);
         setSize(200,200);

         addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
               dispose();
            }
         });
      }

      public boolean action(Event evt, Object arg){
         if(arg.equals("Close")){
            dispose();
            return true;
         }
         return false;
      }

      public void paint(Graphics g){
         g.setColor(Color.white);
         g.drawString("TutorialsPoint.Com", 25,70 );
         g.drawString("Version 1.0", 60, 90);      
      }
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page