Program to demonstrate working of JOptionPane in Java using Swing API
package com.hubberspot.code;
// Import swing package for using JOptionPane
import javax.swing.*;
public class JOptionPaneDemo {
public static void main(String[] args) {
// JOptionPane.showInputDialog shows a simple message along
// with an Input box where user enters and gets it back
// it as a String value
String name = JOptionPane.showInputDialog(null,
"Who is this visitor to Hubberspot ?.");
// JOptionPane.showConfirmDialog shows a simple message
// and Yes/No/Cancel small buttons for user to choose a
// choice. The selected choice is returned back as a
// int having following values :
// 1. JOptionPane.YES_OPTION if user clicked 'Yes'
// 2. JOptionPane.NO_OPTION if user clicked 'No'
// 3. JOptionPane.CANCEL_OPTION if user clicked 'Cancel'
//
int option = JOptionPane.showConfirmDialog(null,
"Hi "+ name + ", do you like Hubberspot ?.");
if(option == JOptionPane.YES_OPTION) {
// JOptionPane.showMessageDialog shows a dialog box
// having a simple message for the user
JOptionPane.showMessageDialog(null,
"Thank you so much !!!");
}
else {
JOptionPane.showMessageDialog(null,
"So like us !!!");
}
}
}
Output of the program :
1. JOptionPane's Input Dialog :
2. JOptionPane's Confirm Dialog :
3. JOptionPane's Message Dialog :


