by Dinesh Thakur Category: Swing

JDialog : Dialog boxes are JFrame restricted dependent on a main JFrame. The general JDialog dialog boxes are normally used for data requests.

A dialog box is a window that folds out another window. It is supposed to deal with a specific issue without cluttering the original window with these details. Dialog boxes are widely used in windowed programming environments, but less used in applets.
To create a dialog box, you inherit from JDialog, which is simply other window, like a JFrame. A JDialog has a handler path (which defaults to BorderLayout) and listener added events to treat events. A significant difference when is called windowClosing () is that you do not want to close the application. in Instead, the resources used by the dialog are released calling dispose ().
Once JDialog is created, the show () method must be called to deploy and activate it. For the dialog box is closed to Call dispose ().

The default layout for a dialog box is border layout. A dialog box is an object of the JDialog class which extends the AWT dialog java.awt.Dialog.JDialog uses a JRootPane as its container.

Some of the constructors defined by JDialog class are as follows:

JDialog ()

JDialog(Frame owner)

JDialog(Frame owner, boolean mode)

JDialog(Frame owner, String title)

JDialog(Frame owner, String title, boolean mode)

where,

owner is the parent frame of the dialog box created

mode specifies the mode of the dialog box as modal or non-modal

title is the title of the dialog box

A dialog box uses a content pane as container to add all the components. The general form of adding a component to the content pane is:

dialogBox.getContentPane().add(component);

There are two types of dialog box, namely, modal and non-modal.

• Modal dialog box: It is the dialog box that does not allow accessing other parts of application until it is closed. It is mainly used for displaying important messages, such as, "Do you really want to close the file?", etc.

• Non-modal dialog box: It is the dialog box that allows accessing other parts of the application even when it is active. Another window can also be activated while this dialog box is being displayed.


Here's a simple example:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE = JavaExampleDialogInputInJApplet.class WIDTH = 370 HEIGHT = 300> </APPLET>*/
public class JavaExampleDialogInputInJApplet extends JApplet implements ActionListener
   {
      JLabel Lbl = new JLabel("Enter the Words");
      JTextField Txt = new JTextField(17);
      JButton BtnShw = new JButton("Show Dialog"),
      BtnOk = new JButton("OK"),
      BtnCncl = new JButton("Cancel");
      private JDialog Dlg = new JDialog((Frame) null,"Dialog", true);
      public void init()
          {
             Container cntnr = getContentPane();
             Container DlgCntnr = Dlg.getContentPane();
             cntnr.setLayout(new FlowLayout());
             cntnr.add(BtnShw);
             DlgCntnr.setLayout(new FlowLayout());
             DlgCntnr.add(Lbl);
             DlgCntnr.add(Txt);
             DlgCntnr.add(BtnOk);
             DlgCntnr.add(BtnCncl);
             BtnShw.addActionListener(this);
             BtnOk.addActionListener(this);
             BtnShw.addActionListener(this);
        }
            public void actionPerformed(ActionEvent e1)
            {
               if(e1.getSource() == BtnShw)
               {
                      Dlg.setBounds(220, 220, 220, 170);
                      Dlg.show();
                   }
                 else if(e1.getSource() == BtnOk)
                         {
                                showStatus(Txt.getText());
                                Dlg.dispose();
                             }
                                else if(e1.getSource() == BtnCncl)
                                 {
                                             showStatus("You Pressed Cancel");
                                             Dlg.dispose();
                                         }
         }
  }

Java Dialog Box Input Example





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles