Java JDialog

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JDialog

The JDialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Dialog class.

Unlike JFrame, it doesn't have maximize and minimize buttons.

JDialog class declaration

Let's see the declaration for javax.swing.JDialog class.

  1. public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer  

Commonly used Constructors:

Constructor

Description

JDialog()

It is used to create a modeless dialog without a title and without a specified Frame owner.

JDialog(Frame owner)

It is used to create a modeless dialog with specified Frame as its owner and an empty title.

JDialog(Frame owner, String title, boolean modal)

It is used to create a dialog with the specified title, owner Frame and modality.

Java JDialog Example

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class DialogExample {  
  5.     private static JDialog d;  
  6.     DialogExample() {  
  7.         JFrame f= new JFrame();  
  8.         d = new JDialog(f , "Dialog Example"true);  
  9.         d.setLayout( new FlowLayout() );  
  10.         JButton b = new JButton ("OK");  
  11.         b.addActionListener ( new ActionListener()  
  12.         {  
  13.             public void actionPerformed( ActionEvent e )  
  14.             {  
  15.                 DialogExample.d.setVisible(false);  
  16.             }  
  17.         });  
  18.         d.add( new JLabel ("Click button to continue."));  
  19.         d.add(b);   
  20.         d.setSize(300,300);    
  21.         d.setVisible(true);  
  22.     }  
  23.     public static void main(String args[])  
  24.     {  
  25.         new DialogExample();  
  26.     }  
  27. }  

Output:

Java Jdialog 1