Java Swing Tutorials - Herong's Tutorial Examples - Version 4.11, by Dr. Herong Yang
showConfirmDialog() - Displaying Confirmation Dialog Boxes
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:
Here is an example program I wrote to test the showConfirmDialog() method:
/* JOptionPaneShowConfirmDialog.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ 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:
Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.
Last update: 2014.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
javax.swing.JOptionPane - Creating and Displaying Option Dialog Boxes
showMessageDialog() - Displaying Message Dialog Boxes
►showConfirmDialog() - Displaying Confirmation Dialog Boxes
Receiving Inputs from Confirmation Dialog Boxes
showInputDialog() - Displaying Input Dialog Boxes
showOptionDialog() - Displaying Option Dialog Boxes
showInternal*Dialog() - Displaying Internal Dialog Boxes
createDialog() - Creating Dialog Boxes Directly
JEditorPane - The Editor Pane Class