- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Confirmation Dialog Box in Java?
To create a confirmation dialog box in Java, use the Java Swing JOptionPane.showConfirmDialog() method, which allows you to create a dialog box that asks for confirmation from the user. For example, Do you want to restart the system?, “This file contains a virus, Do you want to still download?”, etc. Kt comes with a type JOptionPane.YES_NO_CANCEL_OPTION for the same confirmation.
The following is an example to create a Confirmation Dialog Box in Java −
Example
package my; import java.awt.Dimension; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.UIManager; public class SwingDemo { public static void main(String[] args) { ImageIcon icon = new ImageIcon("E −\
ew.PNG"); JPanel panel = new JPanel(); panel.setSize(new Dimension(250, 100)); panel.setLayout(null); JLabel label1 = new JLabel("The file may contain virus."); label1.setVerticalAlignment(SwingConstants.BOTTOM); label1.setBounds(20, 20, 200, 30); label1.setHorizontalAlignment(SwingConstants.CENTER); panel.add(label1); JLabel label2 = new JLabel("Do you still want to save it?"); label2.setVerticalAlignment(SwingConstants.TOP); label2.setHorizontalAlignment(SwingConstants.CENTER); label2.setBounds(20, 80, 200, 20); panel.add(label2); UIManager.put("OptionPane.minimumSize", new Dimension(400, 200)); int res = JOptionPane.showConfirmDialog(null, panel, "File", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon); if(res == 0) { System.out.println("Pressed YES"); } else if (res == 1) { System.out.println("Pressed NO"); } else { System.out.println("Pressed CANCEL"); } } }
Output
Now, let’s say we clicked on the Yes button. In that case, we have displayed the following on Console. Here, 0 is returned for YES, 1 for NO and 2 for CANCEL −
- Related Articles
- How to create Dialog Box in ReactJS?
- How to create a Custom Dialog box on Android?
- How to create a Dialog Box without a title in Android?
- How to create a Dialog Box without a title in Android using Kotlin?
- How can I create a dialog box in Java with Yes No and cancel buttons?
- How to create a Custom Dialog box on iOS App using Swift?
- How to create a Box Layout in Java?
- How To Raise a "File Download" Dialog Box in Python?
- How to create a delete confirmation modal with CSS and JavaScript?
- How to create a Dialog in JavaFX?
- How do we define a dialog box in HTML?
- How to add check box list in alert dialog?
- How to work with Alerts Dialog box in ReactNative?
- How to show prompt dialog box using JavaScript?
- How to create a modal dialog in tkinter?

Advertisements