Updated March 16, 2023
Introduction to JDialog in Java
Java Swings which is a buzzword nowadays contains several functionalities to provide interactive UI (User Interface) for desktop applications. JDialog control is present as part of Java Swings. This control was primarily seen after the introduction of JAVA JDK 1.2, which had multiple other controls added in the kit.
Java Swing has a lightweight and heavyweight containers (majorly lightweight) which differentiates and marks its superiority over its predecessor Java AWT in terms of flexibility and interactivity. A lightweight component in reference to Swing means swing code runs independently of native/machine code while heavyweight depends upon native code. Lightweight components make Swing applications portable and fast.
Swing has 4 top-level heavyweight containers. They are mentioned below:
- JFrame
- JApplet
- JDialog
- JWindow
The purpose of these containers is to group all lightweight components together at one window.
JDialog class works as a general-purpose dialog which is used as a base to have multiple lightweight components. The superclass of JDialogs is java.awt.Dialog. JRootPane is its container and so provides a default window “close” button without re-sizable buttons. JDialog class can be summarized as summation 3 containers:
Windows Constants + Root Pane Container +Dialog in java.awt -> JDialog in Swings
Use of Java JDialog
In some applications, information just needs to be displayed with the “OK” button to use so as to get confirmation from him (for example a confirmatory pop up showed before submitting any exam form.) while some applications require advance features like a text editor, checkboxes, radio buttons. These requirements can be fulfilled using JDialogs.
Syntax of JDialog
The syntax for JDialog in Java is explained below with the help of an example: Here we need to import JDialog class libraries from the master class Swing. A new instance of JDialog can be created using “new” operator. In this case, stest is the JDialog created. We can assign various properties to this new JDialog box.
import javax.swing.* // Importing all Java Swing library functions.
public class JDialogSyntax{
public static void main (String[] args){
JDialog stest = new JDialog(); //here we are creating a new JDialog box by invoking JDialog instance using new operator.
stest.setTitle(“JDialog syntax test”); //This sets the title of Dialog box.
stest.setVisible(true); // multiple properties of JDialog can be used as per user requirements.
stest.setSize(300,300); // This property is used to set the size of dialog box.
stest.setLocation(800,40); // This property is used to set the location of dialog box on the screen.
stest.setDefaultCloseOperation(JDialog.DISPOSE ON CLOSE);
}
}
JDialog can be declared as:
public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer
Constructors of Java JDialog
To and assign different characteristics to the JDialog box we have to invoke constructors. Constructors of JDialog are of three types. These are mentioned below:
- public JDialog( ): In this type of constructor we pass a null value as a parameter. This constructor creates a new dialog without a parent frame as there is nothing passed as a parameter for it.
- public JDialog(Dialog owner, String title, Boolean modal, GraphicsConfiguration gc): This creates a new dialog with an owner dialog, dialog title, Boolean value for a modal setting (0 or 1) and graphics configuration settings.
- public JDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc): This creates a new dialog with an owner frame, dialog title, Boolean value for a modal setting (0 or 1) and graphics configuration settings.
The GraphicsConfiguration setting allows users to select the device to display on which the Dialog should appear in case an application is running on multiple screens.
Common Methods in Java JDialog
Some common methods for JDialog are explained below:
- public JDialog CreateDialog(Component parentcomponent, String title): This method creates a new Dialog box which contains JOptionPane. The input string is used as a title for the dialog box. This frame’s parent is the component received as a parameter. This is one of the most used methods called by all other static methods like “show dialog” methods.
- public int showDialog(Component parentComponent, String showButtonText): This method is used to make dialog visible. This method returns a value as ACCEPT_OPTION, CANCEL_OPTION or ERROR_OPTION according to the user inputs. These are used if the user accepts, cancels or closes the dialog respectively. showDialog( ) can be used to create a customized dialog box with output text specified by the user for the OK button.
- public int showOpenDialog(Component parentComponent): We can use this method to open a dialog box with open on the approve button. These dialogs behave according to the parent component.
- public int showSaveDialog(Component parentComponent): We can use this method to open a dialog box with save on the approve button. These dialogs behave according to the parent component.
Example of Java JDialog
Below example is written in Netbeans IDE 8.2:
import java.awt.event.*;
import javax.swing.*;
class test extends JFrame implements ActionListener {
static JFrame frame;
public static void main(String[] args)
{
frame = new JFrame("JFrame");
test t = new test();
JPanel panel = new JPanel();
JButton button = new JButton("click here to see dialog box");
button.addActionListener(t);
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.show();
}
@Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("click here to see dialog box")) {
JDialog dialog = new JDialog(frame, "JDialog Box");
JLabel lab = new JLabel("This is a dialog box inside frame..");
dialog.add(lab);
dialog.setSize(300, 300);
dialog.setVisible(true);
}
} }
Output:
Properties of Java JDialog
JDialog can also be non-model. Non-model means that the user can interact with other application windows even with JDialog box running. It is not necessary to close the JDialog and then access other applications.
JDialog has multiple properties listed below:
- defaultCloseOperation
- JMenuBar
- Modal
- rootPane
- title
- parent
defaultCloseOperation depicts the behavior of dialog if the window is closed. The parent and title properties listed here can be set in the JDialog constructors according to user preference. Modal property can be set yes or no in JDialog constructor. JMenuBar property can be used if we have to include a menu bar and menu in the Dialog box.
Conclusion
JDialog is one of the important features of JAVA Swing contributing to interactive desktop-based applications. This is used as a top-level container on which multiple lightweight JAVA swing components can be placed to form a window based application.
Recommended Articles
This is a guide to JDialog in Java. Here we discuss its uses, syntax, constructs, properties and common methods with an example of JDialog in Java. You may also look at the following articles to learn more –