Last Updated on by Sarina Sindurakar
Dialog Boxes
A dialog control is a top-level window with a title and a border that is used to receive user input or to offer information to users. They don’t have menu bars, but they behave similarly to frame windows in other ways.
Creating Dialog boxes
A dialog window is a separate sub-window from the main Swing Application Window that is used to convey temporary information. Most dialogs display an error message or a warning to the user, but for dialogs, we use the JOptionPane class. JColorChooser and JFileChooser are two more classes that provide typical dialogs.
Using JOptionPane
You can quickly create and customize various different types of dialogs using JOptionPane. Laying out standard dialogs, supplying icons, specifying the dialog title and text, and changing the button text are all supported by JOptionPane. When a modal Dialog is visible, it prevents user input from reaching any other windows in the program.
JDialogs that are modal are rated by JOptionPane.You must use the JDialog class directly to create a non-modal Dialog.Icon support in JOptionPane allows you to easily define the icon the dialog displays.We may create many types of dialog boxes using the JOptionPane class, as shown below:
Confirm Dialog
The JOptionPanes showConfirmDialog() method can be used to produce a confirm dialog box. These dialog boxes elicit a yes/no/cancel response to a confirmation query. The following signature can be used with this method:
JOptionPane.showConfirmDialog(parent_frame.msg,title,msg_options)
Message options can be one of the following:
- DEFAULT-OPTION
- YES_NO_OPTION
- YES_NO_CANCEL_OPTION
- OK_CANCEL_OPTION
Example:
package swings_2;
import javax.swing.JOptionPane;
public class ConfirmDialog1 {
public static void main(String[] args) {
int input = JOptionPane.showConfirmDialog(null, “Do you like apple?”);
// 0=yes, 1=no, 2=cancel
System.out.println(input);
}
}
Output:
Message Dialog
The JOptionPanes showMessageDialog() method can be used to produce a message dialog box. These dialog boxes inform the user of an event that has occurred. The following signatures can be used with this method:
JOptionPane.showMessageDialog(parent_frame.msg,title,msg_types,icon)
Message type can be of following signature:
- INFORMATION-MESSAGE
- ERROR_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
Example:
//default title and icon
JOptionPane.showMessageDialog(frame, “Eggs are not supposed to be green.”);
Output:
Input Dialog:
We can create input dialog box by calling JOptionPanes showInputDialog method.These dialog prompt users for some input.this method can be used with following signatures:
JOptionPane.showInputDialog(parent_frame,msg,title)
Example:
String m = JOptionPane.showInputDialog(“Anyone there?”);
Output:
Option Dialog
The JOptionPanes showOptionDialog() method can be used to produce an option dialog box. These dialog boxes allow us to create customized dialog boxes by combining the above three dialog boxes. The following signatures can be used with this method:
JOptionPanes.showOptionDialog(parent_frame.msg,title,option-type,msg_types,icon,options,default_option)
Example:
Object[] options = {“Yes, please”, “No, thanks”, “No eggs, no ham!”}; int n = JOptionPane.showOptionDialog(frame, “Would you like some green eggs to go ” + “with that ham?”, “A Silly Question”, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
Output:
Creating Dialog Boxes with JDialog
It is simple to construct dialog boxes with the JOptionPane class, but we must utilize the JDialog class to make custom dialog boxes, as seen below. The JDialog class provides the foundation for all customized dialog boxes in Swing. Any of the constructors defined in the JDialog class can be used to generate dialog boxes.
- JDialog()
- JDialog(parent_frame)
- JDialog(parent_frame,modal)
- JDialog(parent_frame,title)
- JDialog(parent_frame,title,modal)
Example:
package swings_2;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class dialogbox 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
dialogbox s = new dialogbox();
// 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:
Creating Color Choosers
The JColorChooser class can be used to allow users to select colors from a palette. A color selector is a component that may be positioned anywhere in our program. GUI. The JColorChooser API also makes it simple to display a color chooser in a model or non-modal dialog. We can make a color picker using any of the following constructors:
- JColorChooser();
- JColorChooser(initial_color);
After creating a JColorChooser object, we can use the showDialog() method to display it in a dialog box. This method’s signature is as follows:
Color showDialog(Parent,title,inital_color)
Example:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ColorChooserExample extends JFrame implements ActionListener {
JButton b;
Container c;
ColorChooserExample(){
c=getContentPane();
c.setLayout(new FlowLayout());
b=new JButton(“color”);
b.addActionListener(this);
c.add(b);
}
public void actionPerformed(ActionEvent e) {
Color initialcolor=Color.RED;
Color color=JColorChooser.showDialog(this,”Select a color”,initialcolor);
c.setBackground(color);
}
public static void main(String[] args) {
ColorChooserExample ch=new ColorChooserExample();
ch.setSize(400,400);
ch.setVisible(true);
ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:
Creating File Choosers
File choosers provide a graphical user interface for exploring the file system and then selecting a file or directory from a list or typing in a file or directory name. It can be added to any part of our GUI. The JFileChooser API is commonly used to display a file chooser in a modal dialog box. We can make file choosers with any of the builders listed below:
- JFileChooser()
- JFileChooser(file_object)
- JFileChooser(intial_path)
Once an object of JcolorChooser class is created,we can display it in a dialog box by calling its showDialog() or showOpenDialog() or showSaveDialog() methods.Signature of these methods is an below:
- Int showOpenDialog(parent)
- Int showSaveDialog(parent)
- Int show Dialog(parent,title)
Example:
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
// Jlabel to show the files user selects
static JLabel l;
// a default constructor
filechooser()
{
}
public static void main(String args[])
{
// frame to contains GUI elements
JFrame f = new JFrame(“file chooser”);
// set the size of the frame
f.setSize(400, 400);
// set the frame’s visibility
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// button to open save dialog
JButton button1 = new JButton(“save”);
// button to open open dialog
JButton button2 = new JButton(“open”);
// make an object of the class filechooser
filechooser f1 = new filechooser();
// add action listener to the button to capture user
// response on buttons
button1.addActionListener(f1);
button2.addActionListener(f1);
// make a panel to add the buttons and labels
JPanel p = new JPanel();
// add buttons to the frame
p.add(button1);
p.add(button2);
// set the label to its initial value
l = new JLabel(“no file selected”);
// add panel to the frame
p.add(l);
f.add(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
// if the user presses the save button show the save dialog
String com = evt.getActionCommand();
if (com.equals(“save”)) {
// create an object of JFileChooser class
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
// invoke the showsSaveDialog function to show the save dialog
int r = j.showSaveDialog(null);
// if the user selects a file
if (r == JFileChooser.APPROVE_OPTION)
{
// set the label to the path of the selected file
l.setText(j.getSelectedFile().getAbsolutePath());
}
// if the user cancelled the operation
else
l.setText(“the user cancelled the operation”);
}
// if the user presses the open dialog show the open dialog
else {
// create an object of JFileChooser class
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
// invoke the showsOpenDialog function to show the save dialog
int r = j.showOpenDialog(null);
// if the user selects a file
if (r == JFileChooser.APPROVE_OPTION)
{
// set the label to the path of the selected file
l.setText(j.getSelectedFile().getAbsolutePath());
}
// if the user cancelled the operation
else
l.setText(“the user cancelled the operation”);
}
}
}
Output: