• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » Java JDialog Box Input Example
Next →
← Prev

Java JDialog Box Input Example

By Dinesh Thakur

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

You’ll also like:

  1. Dialog Box in Java Example
  2. Confirm Dialog Box Java Swing Example
  3. What is a message box? Type Of message box
  4. Java Input Program Example
  5. Write a java program to get the input through the keyboard using scanner class.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW