Java Swing | JDialog with examples


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:

  1. JDialog() : creates an empty dialog without any title or any specified owner
  2. JDialog(Frame o) :creates an empty dialog with a specified frame as its owner
  3. JDialog(Frame o, String s) : creates an empty dialog with a specified frame as its owner
    and a specified title
  4. JDialog(Window o) : creates an empty dialog with a specified window as its owner
  5. JDialog(Window o, String t) : creates an empty dialog with a specified window as its owner and specified title.
  6. JDialog(Dialog o) :creates an empty dialog with a specified dialog as its owner
  7. JDialog(Dialog o, String s) : creates an empty dialog with a specified dialog as its owner and specified title.

Commonly used methods



  1. setLayout(LayoutManager m) : sets the layout of the dialog to specified layout manager
  2. setJMenuBar(JMenuBar m) : sets the menubar of the dialog to specified menubar
  3. add(Component c): adds component to the dialog
  4. isVisible(boolean b): sets the visibility of the dialog, if value of the boolean is true then visible else invisible
  5. update(Graphics g) : calls the paint(g) function
  6. remove(Component c) : removes the component c
  7. getGraphics() : returns the graphics context of the component.
  8. getLayeredPane() : returns the layered pane for the dialog
  9. setContentPane(Container c) :sets the content pane for the dialog
  10. setLayeredPane(JLayeredPane l) : set the layered pane for the dialog
  11. setRootPane(JRootPane r) : sets the rootPane for the dialog
  12. getJMenuBar() : returns the menubar of the component
  13. setTransferHandler(TransferHandler n) : Sets the transferHandler property, which is a mechanism to support transfer of data into this component.
  14. setRootPaneCheckingEnabled(boolean enabled) : Sets whether calls to add and setLayout are forwarded to the contentPane.
  15. setRootPane(JRootPane root) :Sets the rootPane property of the dialog.
  16. setGlassPane(Component glass) : Sets the glassPane property of the dialog.
  17. repaint(long time, int x, int y, int width, int height): Repaints the specified rectangle of this component within time milliseconds.
  18. remove(Component c): Removes the specified component from the dialog.
  19. isRootPaneCheckingEnabled() : Returns whether calls to add and setLayout are forwarded to the contentPane or not .
  20. getTransferHandler() : returns the transferHandler property.
  21. getRootPane() : Returns the rootPane object for this dialog.
  22. getGlassPane() : Returns the glassPane object for this dialog.
  23. createRootPane() : Called by the constructor methods to create the default rootPane.
  24. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

2. Program to create a dialog within a dialog

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 loaction of dialog
            d1.setLocation(200, 200);
  
            // set visibility of dialog
            d1.setVisible(true);
        }
    }
}

chevron_right


Output :

Note : The above programs might not run in an online compiler please use an offline IDE



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.