Swing Dialog Box in Java

Swing Dialog Boxes in Java with Examples

In this article, I am going to discuss Swing Dialog boxes in Java with Examples. Please read our previous article, where we discussed Swing Controls in Java. At the end of this article, you will understand the different Dialog Boxes that are available in Java Swings with Examples.

Dialog box : JOptionPane

Dialog Box is a small pop-up box that was used for different cases like confirmation, warning, message, etc. Dialog boxes are of three types:

  1. Confirmation
  2. Input
  3. Message

Swing package provides a special class i.e. JOptionPane which was used for creating the above dialog boxes as ready-made.

Declaration: public class JOptionPane extends JComponent implements Accessible

JOptionPane Constructors
  1. JOptionPane(): Creates a JOptionPane with a test message.
  2. JOptionPane(Object message): Creates an instance of JOtionPane to display a message.
  3. JOptionPane(Object message, int messageType): Creates an instance of JOtiopnPane to display a message with specified message type and default options.
JOptionPane Methods

Some of the static methods in the JOptionPane class are as follows:

  1. showConfirmDialog(Component parentComponent, Object message): Creates a dialog box with the option Yes, No, and Cancel; with the title, Select an Option.
  2. showInputDialog(Component parentComponent, Object message): It show a question-message dialog box requesting input from the user parented to parent component.
  3. showMessageDialog(Component parentComponent, Object message): Creates an information-message dialog box titled “Message”.
Example for Message Dialog Box in Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JOptionPaneDemo
{
    static JTextField jf1;
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setSize (250, 250);
        FlowLayout ob = new FlowLayout ();
        jf.setLayout (ob);
        JLabel j1 = new JLabel ("Enter Name");
        jf1 = new JTextField (10);
        JButton jb = new JButton ("Submit");
        MyListener1 ob1 = new MyListener1 ();
        jb.addActionListener (ob1);
        jf.add (j1);
        jf.add (jf1);
        jf.add (jb);
        jf.setVisible (true);
    }
}
class MyListener1 implements ActionListener
{
    public void actionPerformed (ActionEvent ae)
    {
        String name = JOptionPaneDemo.jf1.getText ();
        JOptionPane.showMessageDialog (null, "Welcome," + name);
    }
}
Output:

Swing Dialog Boxes in Java with Examples

The appeared Dialog Box looks like this:

Message Dialog Box in Java

Example for Confirmation Dialog Box in Java:
import javax.swing.*;
import java.awt.event.*;

public class DialogBoxDemo
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setSize (100, 100);
        JButton jb = new JButton ("Exit");
        jf.add (jb);
        MyListener2 ob = new MyListener2 ();
        jb.addActionListener (ob);
        jf.setVisible (true);
    }
}
class MyListener2 implements ActionListener
{
    public void actionPerformed (ActionEvent ae)
    {
        int i =JOptionPane.showConfirmDialog (null, "Are you sure to exit?",
         "Confirmation",
         JOptionPane.YES_NO_OPTION);
        if (i == 0)
        System.exit (0);
    }
}

Output:

Example for Confirmation Dialog Box in Java

The appeared Dialog Box looks like this:

Swing Dialog boxes in Java with Examples

In the next article, I am going to discuss Working with Images, Menus & Files in Java Swings with examples. Here, in this article, I try to explain the Swing Dialog boxes in Java with Examples and I hope you enjoy this Swing Dialog Box in Java with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *