This section provides a tutorial example on how to use the static method, showConfirmDialog(), to create and display confirmation dialog boxes for 3 different types of options and 4 different types of messages: information, warning, error, and plain.
The second type of dialog boxes you can create and display with the javax.swing.JOptionPane class is the confirmation dialog box.
This can be done with the static method: showConfirmDialog(frame, message, title, option, type), where:
"frame" is a frame object to be used as the parent frame.
"message" is the message string to be display on the dialog box.
"title" is the title string to be used as the dialog box title.
"option" is an integer code representing a specific confirmation option type. Valid type codes are predefined
as constants in the JOptionPane class: YES_NO_OPTION, YES_NO_CANCEL_OPTION, and OK_CANCEL_OPTION.
"type" is an integer code representing a specific message dialog box type. Valid type codes are predefined
as constants in the JOptionPane class: INFORMATION_MESSAGE, WARNING_MESSAGE, ERROR_MESSAGE and PLAIN_MESSAGE.
Here is an example program I wrote to test the showConfirmDialog() method:
/**
* JOptionPaneShowConfirmDialog.java
* Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JOptionPaneShowConfirmDialog implements ActionListener {
JFrame myFrame = null;
int optionType = JOptionPane.YES_NO_OPTION;
int messageType = JOptionPane.INFORMATION_MESSAGE;
public static void main(String[] a) {
(new JOptionPaneShowConfirmDialog()).test();
}
private void test() {
myFrame = new JFrame("showConfirmDialog() Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container myPane = myFrame.getContentPane();
myPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.CENTER);
myPane.add(getFieldPanel(),c);
setMyConstraints(c,0,1,GridBagConstraints.CENTER);
myPane.add(getButtonPanel(),c);
myFrame.pack();
myFrame.setVisible(true);
}
private JPanel getFieldPanel() {
JPanel p = new JPanel(new GridBagLayout());
p.setBorder(BorderFactory.createTitledBorder("Settings"));
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.EAST);
p.add(new JLabel("Option Type:"),c);
setMyConstraints(c,1,0,GridBagConstraints.WEST);
p.add(getOptionPanel(),c);
setMyConstraints(c,0,1,GridBagConstraints.EAST);
p.add(new JLabel("Message type:"),c);
setMyConstraints(c,1,1,GridBagConstraints.WEST);
p.add(getMessagePanel(),c);
return p;
}
private JPanel getOptionPanel() {
JPanel myPanel = new JPanel(new GridBagLayout());
ButtonGroup myGroup = new ButtonGroup();
JRadioButton myButton = new JRadioButton("Yes-No",true);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
myButton = new JRadioButton("Yes-No-Cancel",false);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
myButton = new JRadioButton("Ok-Cancel",false);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
return myPanel;
}
private JPanel getMessagePanel() {
JPanel myPanel = new JPanel(new GridBagLayout());
ButtonGroup myGroup = new ButtonGroup();
JRadioButton myButton = new JRadioButton("Information",true);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
myButton = new JRadioButton("Warning",false);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
myButton = new JRadioButton("Error",false);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
myButton = new JRadioButton("Plain",false);
myButton.addActionListener(this);
myGroup.add(myButton);
myPanel.add(myButton);
return myPanel;
}
private JPanel getButtonPanel() {
JPanel p = new JPanel(new GridBagLayout());
JButton myButton = new JButton("Show");
myButton.addActionListener(this);
p.add(myButton);
return p;
}
public void actionPerformed(ActionEvent e) {
String cmd = ((AbstractButton) e.getSource()).getText();
System.out.println("Button clicked: "+cmd);
if (cmd.equals("Information")) {
messageType = JOptionPane.INFORMATION_MESSAGE;
} else if (cmd.equals("Warning")) {
messageType = JOptionPane.WARNING_MESSAGE;
} else if (cmd.equals("Error")) {
messageType = JOptionPane.ERROR_MESSAGE;
} else if (cmd.equals("Plain")) {
messageType = JOptionPane.PLAIN_MESSAGE;
} else if (cmd.equals("Yes-No")) {
optionType = JOptionPane.YES_NO_OPTION;
} else if (cmd.equals("Yes-No-Cancel")) {
optionType = JOptionPane.YES_NO_CANCEL_OPTION;
} else if (cmd.equals("Ok-Cancel")) {
optionType = JOptionPane.OK_CANCEL_OPTION;
} else if (cmd.equals("Show")) {
JOptionPane.showConfirmDialog(myFrame,
"Confirmation dialog box text message.",
"Confirmation Dialog Box", optionType, messageType);
}
}
private void setMyConstraints(GridBagConstraints c,
int gridx, int gridy, int anchor) {
c.gridx = gridx;
c.gridy = gridy;
c.anchor = anchor;
}
}
If you run this example, select an option type and a message type,
then click the "Show" button,
you will see a confirmation dialog box showing up like this:
Interesting notes about this tutorial example:
I used several panels and layouts to organize the settings area.
An action listener has been added to all radio buttons and the "Show" button.
The actionPerformed() method in the action listener is implemented to handle all changes.
What is missing in this example is how to catch input on option buttons on the confirmation dialog box.
Sample programs listed in this section have been tested with JDK 1.6.0.