What Will I Learn?
<ul>
<li>The user will learn about Confirm Dialog Box Method.
<li>The user will learn how to use Confirm Dialogs to make better decisions.
<h4>Requirements
<ul>
<li>A computer System is required for this tutorial.
<li>Java Software Development Kit(JDK) must be installed on your computer.
<li>An Integrated Development Environment(IDE) such as Eclipse or NetBeans is required to be installed on your computer.
<p dir="auto">Get JDK <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here
<p dir="auto">Get Eclipse <a href="http://www.eclipse.org/downloads/eclipse-packages/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here
<p dir="auto">Get NetBeans <a href="https://netbeans.org/downloads/index.html" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here
<h4>Difficulty
<p dir="auto">Intermediate.
<h4>Tutorial Contents
<p dir="auto">This method is a quick and easy way to get user input by asking a confirming question, like yes/no/cancel . The showConfirmDialog() can be called using the following combinations of parameters:
<pre><code>Component, Object
Component, Object, String, int
Component, Object, String, int, int
Component, Object, String, int, int, Icon
<p dir="auto"><strong>Component – The first parameter is a Component which determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used.
<p dir="auto"><strong>Object – The second parameter can be any Object. (In some older versions of Java you might get a compiler error when using primitive types directly)
<p dir="auto"><strong>String – The third parameter is a String placed as the title of the confirmDialog window.
<p dir="auto"><strong>int – The int that follows the String is the OptionType. The different OptionTypes for JOptionPane, are:
<ul>
<li>DEFAULT_OPTION
<li>YES_NO_OPTION
<li>YES_NO_CANCEL_OPTION
<li>OK_CANCEL_OPTION
<p dir="auto"><strong>int – The next int is the MessageType. The different MessageTypes for JOptionPane, are:
<ul>
<li>ERROR_MESSAGE
<li>INFORMATION_MESSAGE
<li>WARNING_MESSAGE
<li>QUESTION_MESSAGE
<li>PLAIN_MESSAGE
<p dir="auto"><strong>Icon – The last parameter is an Icon that is displayed inside the dialog and overrides the default MessageTypeicon.
<p dir="auto">In this tutorial, we will create two classes;
<ul>
<li><p dir="auto">Class 1: We will create two JList objects, Inventory and Foods list and a JButton object. Inventory list will contain items of two different categories; Foods list will be empty. The user will select an item from Inventory list to add to Foods list, when the button is clicked, a Confirmation message pops up asking the user to confirm decision. After confirmation, the items are added or left out.
<li><p dir="auto">Class 2: Contains main method for run instructions.
<p dir="auto">This tutorial is done in assumption that the user follows through previous tutorials and is familiar with the coding terms used therein, also, have basic Java programming knowledge.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100504/fvqm45yjiibj2v3omfni.png" alt="1.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100504/fvqm45yjiibj2v3omfni.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100504/fvqm45yjiibj2v3omfni.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100511/spry4nmopo7mz1jmz4z4.png" alt="2.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100511/spry4nmopo7mz1jmz4z4.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100511/spry4nmopo7mz1jmz4z4.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100518/tiqtj6cfq88guh6sbvag.png" alt="3.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100518/tiqtj6cfq88guh6sbvag.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520100518/tiqtj6cfq88guh6sbvag.png 2x" />
<h1>CODE BLOCK
<h6>confirmDialog Class
<pre><code>import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
<pre><code>public class confirmDialog extends JFrame {
<pre><code> private JPanel panel;
private JButton toButton;
private JLabel label1;
private JLabel label2;
private JList inventory;
private JList foods;
<pre><code> String[] list = {"Wire", "Fudge", "Toothbrush", "Ketchup",
"Telephone", "Bottle", "Burger", "Potato",
"Television", "Taco"};
<pre><code> public confirmDialog() {
super("Confirm Dialog Demo");
setLayout(null);
<pre><code> panel = new JPanel();
panel.setBackground(Color.BLACK);
getContentPane().setBackground(Color.ORANGE);
add(panel);
<pre><code> label1 = new JLabel("Inventory List");
label1.setFont(new Font("Castellar", Font.BOLD, 15));
label1.setBounds(30, 21, 200, 20);
add(label1);
<pre><code> inventory = new JList(list);
inventory.setBackground(Color.YELLOW);
<pre><code> inventory.setBounds(40, 55, 120, 190);
add(inventory);
<pre><code> toButton = new JButton("Add to Foods");
toButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
int option = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm Dialog Box",
JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION)
foods.setListData(inventory.getSelectedValues());
}
}
);
<pre><code> toButton.setBounds(170, 110, 110, 30);
toButton.setBackground(Color.ORANGE);
add(toButton);
<pre><code> label2 = new JLabel("Foods List");
label2.setFont(new Font("Castellar", Font.BOLD, 15));
label2.setBounds(280, 21, 200, 20);
add(label2);
<pre><code> foods = new JList();
foods.setBackground(Color.YELLOW);
foods.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
foods.setBounds(290, 55, 120, 190);
add(foods);
}
}
<h6>confirmMain Class
<pre><code>import javax.swing.JFrame;
<pre><code>public class confirmMain {
<pre><code> public static void main (String args []) {
<pre><code> confirmDialog confirm = new confirmDialog();
confirm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
confirm.setSize(500, 350);
confirm.setVisible(true);
}
}
<h3>Output
<p dir="auto"><img src="https://images.hive.blog/0x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520106265/tgjw88u4ly7zts4jxzjr.gif" alt="ezgif.com-optimize.gif" />
<h1>confirmDialog Class
<pre><code>label1 = new JLabel("Inventory List");
label1.setFont(new Font("Castellar", Font.BOLD, 15));
label1.setBounds(30, 21, 200, 20);
add(label1);
<p dir="auto">A label to describe the Inventory List is created and the font name, type and size is also set using <code>.setFont(), it appears at the top left corner of the screen which is done using <code>.setBounds() and it’s added to the frame. Basically, this names the list.
<pre><code>inventory = new JList(list);
inventory.setBackground(Color.YELLOW);
inventory.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
inventory.setBounds(40, 55, 120, 190);
add(inventory);
<p dir="auto">This creates a JList object. Background color set to Yellow.
<p dir="auto">This is a MULTIPLE INTERVAL SELECTION list. This means that you can select multiple list items at once.
<p dir="auto"><code>.setBounds() sets the location and the list is added to the frame.
<pre><code>toButton = new JButton("Add to Foods");
toButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
int option = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm Dialog Box", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION)
foods.setListData(inventory.getSelectedValues());
}
}
);
<pre><code>toButton.setBounds(170, 110, 110, 30);
toButton.setBackground(Color.ORANGE);
add(toButton);
<p dir="auto">This creates a JButton object with the tag “Add to Foods. This button adds the selected items to the Foods List.<br />
An ActionListener is associated with the button, this interface listens for events so it can execute commands when clicked. For more on ActionListeners and EventHandlers, click my blog post <a href="https://utopian.io/utopian-io/@will-ugo/event-handling-event-and-listener-actionlistener" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here<br />
In the Listener Interface, the <code>.showConfirmDialog() method is used to make decisions. When the button is clicked, a pop up confirmation box appears asking the user to confirm his selection. The dialog has a “Yes” and “No” option.<br />
If the Yes option is clicked, the Foods List is populated with the selected values from Inventory List. This is done using <code>.setListData(inventory.getSelectedValues).<br />
The button is set between the two lists and is added to the frame.
<pre><code>label2 = new JLabel("Foods List");
label2.setFont(new Font("Castellar", Font.BOLD, 15));
label2.setBounds(280, 21, 200, 20);
add(label2);
<p dir="auto">This creates a label to name the Foods List. The font name, type, size and location is set and is added to the frame.
<pre><code>foods = new JList();
foods.setBackground(Color.YELLOW);
foods.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
foods.setBounds(290, 55, 120, 190);
add(foods);
<p dir="auto">This creates a second JList object. Background color set to Yellow.
<p dir="auto">This is a MULTIPLE INTERVAL SELECTION list.
<p dir="auto"><code>.setBounds() sets the location and the list is added to the frame.
<p dir="auto">This List is the destination of the selected values from Inventory list after the button is clicked.
<h1>confirmMain class
<pre><code>import javax.swing.JFrame;
<pre><code>public class confirmMain {
<pre><code>public static void main (String args []) {
<pre><code>confirmDialog confirm = new confirmDialog();
confirm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
confirm.setSize(500, 350);
confirm.setVisible(true);
}
}
<p dir="auto">Main class containing main method with instructions on how the program runs.
<p dir="auto">An object of the Slider class is created. The slider class holds instructions for running the program.
<p dir="auto">The program Exit Mode is set. This will close the program when user clicks the “X” button on the frame.
<p dir="auto">Size of the window is set using inbuilt methods <code>.setSize
<p dir="auto">Visibility setting is set, making the frame visible, the code <code>.setVisible(true) is used.
<p dir="auto"><img src="https://images.hive.blog/0x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520106265/tgjw88u4ly7zts4jxzjr.gif" alt="ezgif.com-optimize.gif" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105403/ovsi318t2lly8bqmi0qy.png" alt="4.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105403/ovsi318t2lly8bqmi0qy.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105403/ovsi318t2lly8bqmi0qy.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105410/binrmfahult9yz5j5ofl.png" alt="5.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105410/binrmfahult9yz5j5ofl.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105410/binrmfahult9yz5j5ofl.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105419/fq5qwcdgmmst4jczcfjn.png" alt="6.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105419/fq5qwcdgmmst4jczcfjn.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105419/fq5qwcdgmmst4jczcfjn.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105429/sfgmbto6sma2ad76jaoq.png" alt="7.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105429/sfgmbto6sma2ad76jaoq.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520105429/sfgmbto6sma2ad76jaoq.png 2x" />
<p dir="auto">The program is run and the two Lists is displayed; Inventory List is populated and Foods List is empty, multiple items are selected holding down the Ctrl button, when the “Add to Foods button” is clicked, a confirmation box pops up asking the user to confirm his decision. After confirmation, the items are added to the Foods List. If “No” is clicked, things remain as they were.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120177/zgzsehwephxngstrvvo4.png" alt="8.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120177/zgzsehwephxngstrvvo4.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120177/zgzsehwephxngstrvvo4.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120190/fdtcuh8swnroys456ib5.png" alt="9.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120190/fdtcuh8swnroys456ib5.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120190/fdtcuh8swnroys456ib5.png 2x" />
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120206/woqefzgihbrjb6rkp3c5.png" alt="10.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120206/woqefzgihbrjb6rkp3c5.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520120206/woqefzgihbrjb6rkp3c5.png 2x" />
<p dir="auto">Source codes from Github.
<p dir="auto">You can get the codes at my <a href="https://github.com/will-ugo/confirmDialog/tree/master/src" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">GitHub Account if you want to try it on your own.
<h6>REFERENCES
<ul>
<li><a href="https://www.mkyong.com/swing/java-swing-how-to-make-a-confirmation-dialog/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Mkyong
<h4>Curriculum
<p dir="auto">Similar posts already posted on Utopian are:
<ul>
<li><p dir="auto"><a href="https://utopian.io/utopian-io/@will-ugo/java-gui-jspinner-and-functions-using-eclipse-ide" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Java GUI: JSpinner and functions Using Eclipse IDE<br />
<li><p dir="auto"><a href="https://utopian.io/utopian-io/@will-ugo/java-gui-jtabbedpane-and-functions-using-eclipse-ide" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Java GUI: JTabbedPane and Functions Using Eclipse IDE<br />
<ul>
<li><a href="https://utopian-io/utopian-io/@will-ugo/java-graphical-user-interface-gui-jmenubar-jmenu-and-jmenuitem-functions-and-operations-using-eclipse-ide" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Java Graphical User Interface(GUI): JMenuBar, JMenu and JMenuItem functions and operations using Eclipse IDE<br />
<ul>
<li><a href="https://utopian.io/utopian-io/@will-ugo/java-graphical-user-interface-gui-jslider-functions-and-changelistener-interface-using-eclipse-ide" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Java Graphical User Interface(GUI): JSlider Functions and ChangeListener Interface Using Eclipse IDE<br />
<p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@will-ugo/java-gui-confirm-dialogs-using-eclipse-ide" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @will-ugo I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
This post has received a 0.16 % upvote from @drotto thanks to: @banjo.