Java Swing | JDialog with examples Last Updated : 16 Apr, 2021 Comments Improve Suggest changes Like Article Like Report JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need .Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified ownerJDialog(Frame o) :creates an empty dialog with a specified frame as its ownerJDialog(Frame o, String s) : creates an empty dialog with a specified frame as its owner and a specified titleJDialog(Window o) : creates an empty dialog with a specified window as its ownerJDialog(Window o, String t) : creates an empty dialog with a specified window as its owner and specified title.JDialog(Dialog o) :creates an empty dialog with a specified dialog as its ownerJDialog(Dialog o, String s) : creates an empty dialog with a specified dialog as its owner and specified title. Commonly used methods setLayout(LayoutManager m) : sets the layout of the dialog to specified layout managersetJMenuBar(JMenuBar m) : sets the menubar of the dialog to specified menubaradd(Component c): adds component to the dialogisVisible(boolean b): sets the visibility of the dialog, if value of the boolean is true then visible else invisibleupdate(Graphics g) : calls the paint(g) functionremove(Component c) : removes the component cgetGraphics() : returns the graphics context of the component.getLayeredPane() : returns the layered pane for the dialogsetContentPane(Container c) :sets the content pane for the dialogsetLayeredPane(JLayeredPane l) : set the layered pane for the dialogsetRootPane(JRootPane r) : sets the rootPane for the dialoggetJMenuBar() : returns the menubar of the componentsetTransferHandler(TransferHandler n) : Sets the transferHandler property, which is a mechanism to support transfer of data into this component.setRootPaneCheckingEnabled(boolean enabled) : Sets whether calls to add and setLayout are forwarded to the contentPane.setRootPane(JRootPane root) :Sets the rootPane property of the dialog.setGlassPane(Component glass) : Sets the glassPane property of the dialog.repaint(long time, int x, int y, int width, int height): Repaints the specified rectangle of this component within time milliseconds.remove(Component c): Removes the specified component from the dialog.isRootPaneCheckingEnabled() : Returns whether calls to add and setLayout are forwarded to the contentPane or not .getTransferHandler() : returns the transferHandler property.getRootPane() : Returns the rootPane object for this dialog.getGlassPane() : Returns the glassPane object for this dialog.createRootPane() : Called by the constructor methods to create the default rootPane.addImpl(Component co, Object c, int i) : Adds the specified child Component to the dialog. The following programs will illustrate the use of JDialog 1 .Program to create a simple JDialog Java // java Program to create a simple JDialog import java.awt.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame implements ActionListener { // frame static JFrame f; // main class public static void main(String[] args) { // create a new frame f = new JFrame("frame"); // create a object solve s = new solve(); // create a panel JPanel p = new JPanel(); JButton b = new JButton("click"); // add actionlistener to button b.addActionListener(s); // add button to panel p.add(b); f.add(p); // set the size of frame f.setSize(400, 400); f.show(); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals("click")) { // create a dialog Box JDialog d = new JDialog(f, "dialog Box"); // create a label JLabel l = new JLabel("this is a dialog box"); d.add(l); // setsize of dialog d.setSize(100, 100); // set visibility of dialog d.setVisible(true); } } } Output: 2. Program to create a dialog within a dialog Java // java Program to create a dialog within a dialog import java.awt.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame implements ActionListener { // frame static JFrame f; // dialog static JDialog d, d1; // main class public static void main(String[] args) { // create a new frame f = new JFrame("frame"); // create a object solve s = new solve(); // create a panel JPanel p = new JPanel(); JButton b = new JButton("click"); // add actionlistener to button b.addActionListener(s); // add button to panel p.add(b); f.add(p); // set the size of frame f.setSize(400, 400); f.show(); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals("click")) { // create a dialog Box d = new JDialog(f, "dialog Box"); // create a label JLabel l = new JLabel("this is first dialog box"); // create a button JButton b = new JButton("click me"); // add Action Listener b.addActionListener(this); // create a panel JPanel p = new JPanel(); p.add(b); p.add(l); // add panel to dialog d.add(p); // setsize of dialog d.setSize(200, 200); // set visibility of dialog d.setVisible(true); } else { // create a dialog Box d1 = new JDialog(d, "dialog Box"); // create a label JLabel l = new JLabel("this is second dialog box"); d1.add(l); // setsize of dialog d1.setSize(200, 200); // set location of dialog d1.setLocation(200, 200); // set visibility of dialog d1.setVisible(true); } } } Output : Note : The above programs might not run in an online compiler please use an offline IDE Comment More infoAdvertise with us Next Article Java Swing | JDialog with examples andrew1234 Follow Improve Article Tags : Java Programming Language java-swing Practice Tags : Java Similar Reads Java Swing - JPanel With Examples JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar. Constructors of JPanel JPanel 4 min read Java Swing | JWindow with examples JWindow is a part of Java Swing and it can appear on any part of the users desktop. It is different from JFrame in the respect that JWindow does not have a title bar or window management buttons like minimize, maximize, and close, which JFrame has. JWindow can contain several components such as butt 5 min read Java Swing | JList with examples JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .Constructor for JList are : JList(): creates an empty blank listJList(E [ ] 4 min read Java Swing | JSeparator with examples JSeparator is a part of Java Swing framework. It is used to create a dividing line between two components. More specifically, it is mainly used to create dividing lines between menu items in a JMenu. In JMenu or JPopupMenu addSeparartor function can also be used to create a separator. Constructor of 4 min read Java Swing | JComboBox with examples JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .Constructor of the JComboBox 7 min read Java Swing | JCheckBox with examples JCheckBox is a part of Java Swing package . JCheckBox can be selected or deselected . It displays it state to the user . JCheckBox is an implementation to checkbox . JCheckBox inherits JToggleButton class. Constructor of the class are : JCheckBox() : creates a new checkbox with no text or icon JChec 5 min read Java Swing | Internal Frame with examples JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc. Constructors for JInternalFrame JInternalFrame() : creates a new non- closable, non- resizable, non- i 8 min read javap tool in Java with Examples javap tool The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used (â-câ or â-verboseâ for byte code and byte code along with innards info, respective 2 min read Level toString() method in Java with Examples The toString() method of java.util.logging.Level is used to get the string value which represents this Level.This method returns a string representation of this Level. Syntax: public String toString() Parameters: This method accepts nothing. Return: This method returns a string representation of thi 1 min read Java Swing | Popup and PopupFactory with examples Popup and PopupFactory are a part of the Java Swing library. Popups are used when we want to display to the user a Component on the top of all the other Components in that particular containment hierarchy. PopupFactory is the class that is used to create popups. Popups have a very small life cycle, 5 min read Like