0% found this document useful (0 votes)
16 views

WWW Geeksforgeeks Org Java Joptionpane

The document discusses the Java JOptionPane class which is used to display dialog boxes in Java Swing applications. It describes the constructors, methods and fields of JOptionPane and provides examples of how to create message, input, confirmation and option dialogs using JOptionPane.

Uploaded by

saif.bk2019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

WWW Geeksforgeeks Org Java Joptionpane

The document discusses the Java JOptionPane class which is used to display dialog boxes in Java Swing applications. It describes the constructors, methods and fields of JOptionPane and provides examples of how to create message, input, confirmation and option dialogs using JOptionPane.

Uploaded by

saif.bk2019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tutorials DSA Data Science Web Tech Courses Sign In

Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception Handling Java Programs Java Project Java Collections Interview Java Interview Quest

Java JOptionPane
Java JOptionPane
Java JRootPane Last Updated : 07 Nov, 2023
Java JEditorPane In Java, JOptionPane is a part of the Java Swing library. It helps us to create dialog boxes
such as message dialogs, conformation dialogs, input dialogs, and options dialogs In this
Java JDesktopPane
article, we are going to explore some constructors, methods, and some examples of
Java JTextPane JOptionPane.

Java JComponent Constructors of JOptionPane Class

Java JTabbedPane
Constructors Description
Java JScrollPane
JOptionPane() This is the default constructor for JOptionPane.It
Java JTree
is used to create a JOptionPane with no options
Java JLayeredPane and message.

JLink | Java Linker


JOptionPane(Object message) It creates a message dialog with a specified
Java AWT Panel message.

JavaFX | Tooltip
JOptionPane(Object message, int It creates a message dialog with a specified
Java JFrame messageType) message and its Type.

JavaFX | SplitPane Class


JOptionPane(Object message, int It helps us to create a dialog with a specified
JavaFX | TitledPane Class messageType, int optionType) message, message type, and option type.

JavaFX | TabPane Class

JavaFX | Pane Class


Methods of JOptionPane

Java Tutorial
Methods Description
Java Swing | JTable
createDialog(String title) It helps us to create JDialog with a
Java AWT PopupMenu specified title but without any parent.

Java JViewport
showMessageDialog(Component This method displays a message
JavaFX | StackPane Class parentComponent, Object message) dialog with the specified message.

JavaFX | TextInputDialog
showInputDialog(Component This method displays an input dialog
Java JScrollBar parentComponent, Object message) with the specified message.

JRE in Java
setMessageType(int messageType) This method to set the message type
JavaFX Tutorial of the dialog

Java Swing | JMenuBar


setOptionType(int optionType) This method allows you to set the
Java MouseMotionListener in AWT
option type for the dialog.

Java Swing | JTextArea


setOptions(Object[] options) Here we can set a list of custom
options.

setInitialValue(Object initialValue) This method sets the initial selection


when using custom options in the
dialog.

Fields of JOptionPane

Fields Description
Skip to content
int ERROR_MESSAGE A Constant code displaying an error message icon.

int INFORMATION_MESSAGE A Constant code displaying information message icon.

int WARNING_MESSAGE A Constant code for displaying Warning message icon.

int YES_NO_OPTION A constant for creating a dialog with “Yes” and “No”
options.

int A constant for creating a dialog with “Yes,” “No,” and


YES_NO_CANCEL_OPTION “Cancel” options.

int OK_CANCEL_OPTION A constant for creating a dialog with “OK” and “Cancel”
options.

Classes from Which JOptionPane Methods are inherited


java.awt.Component
javax.swing.JComponent
java.io.Serializable

Following are the programs to implement JOptionPane

1. Java program to create a showMessageDialog in JOptionPane

This dialog is used to display messages to the user.

Java

//Java program to create a showMessageDialog in JOptionPane


import javax.swing.JOptionPane;

public class MessageDialogExample {


public static void main(String[] args) {
// Create a JOptionPane to display a message dialog
// The first parameter (null) specifies the dialog's parent componen
// The second parameter is the message to display ("GFG" in this cas
// The third parameter is the title of the dialog ("Geeks Premier Le
// The fourth parameter (JOptionPane.INFORMATION_MESSAGE) specifies
JOptionPane.showMessageDialog(null, "GFG", "Geeks Premier League 202
JOptionPane.INFORMATION_MESSAGE);
}
}

Output:

2. Java Program to create a showInputDialog in JOptionPane

This dialog is used to take input from the user.

Skip to content
Java

//Java Program to create a showInputDialog in JOptionPane


import javax.swing.JOptionPane;

public class InputDialogExample {


public static void main(String[] args) {
// Prompt the user to enter their article name and store it in the '
String name = JOptionPane.showInputDialog("Enter your Article Name:"

// Create a JOptionPane to display a message with a personalized gre

JOptionPane.showMessageDialog(null, "GFG " + name + "!");


}
}

Output:

Final Output After Selection:

3. Java Program to create showConfirmDialog in JOptionPane

This dialog displays a confirmation message and allows the user to make a decision.

Java

//Java Program to create showConfirmDialog in JOptionPane


import javax.swing.JOptionPane;

public class ConfirmDialogExample {


public static void main(String[] args) {
// Display a confirmation dialog with Yes, No, and Cancel options
int choice = JOptionPane.showConfirmDialog(null, "Do you want to sav
"Confirmation", JOptionPa

// Check the user's choice and display a corresponding message


if (choice == JOptionPane.YES_OPTION) {
// If the user chose 'Yes', show a message indicating that chang
JOptionPane.showMessageDialog(null, "Changes saved.");
} else if (choice == JOptionPane.NO_OPTION) {
// If the user chose 'No', show a message indicating that change
Skip to content
JOptionPane.showMessageDialog(null, "Changes not saved.");
} else {
// If the user chose 'Cancel' or closed the dialog, show a messa
JOptionPane.showMessageDialog(null, "Operation canceled.");
}
}
}

Output:

Final Output After Selection:

4. Java Program to create a showOptionDialog in JOptionPane

This dialog allows you to create a customized option dialog.

Java

// Java Program to create a


// showOptionDialog in JOptionPane
import javax.swing.JOptionPane;

// Driver Class
public class OptionDialogExample {
// main function
public static void main(String[] args)
{
// Define an array of custom options for the dialog
Object[] options = { "Yes", "No", "Cancel" };

// Display an option dialog with custom options


// The user's choice is stored in the 'choice'
// variable
int choice = JOptionPane.showOptionDialog(
null, // Parent component (null means center on screen)
"Do you want to proceed?", // Message to display
"Custom Options", // Dialog title
JOptionPane.YES_NO_CANCEL_OPTION, // Option type (Yes, No, Cance
JOptionPane.QUESTION_MESSAGE, // Message type (question icon)
null, // Custom icon (null means no custom icon)
options, // Custom options array
options[2] // Initial selection (default is "Cancel")
);

// Check the user's choice and display a


// corresponding message
if (choice == JOptionPane.YES_OPTION) {
// If the user chose 'Yes'
// show a message indicating that they are
// proceeding
JOptionPane.showMessageDialog(null,"Proceeding...");
}
else if (choice == JOptionPane.NO_OPTION) {
// If the user chose 'No'
Skip to content
// show a message indicating that they are not
// proceeding
JOptionPane.showMessageDialog(null, "Not proceeding.");
}
else {
// If the user chose 'Cancel' or closed the
// dialog
// show a message indicating the operation is
// canceled
JOptionPane.showMessageDialog(null, "Operation canceled.");
}
}
}

Output:

Final Output After Selection:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master
backend development efficiently and on schedule.
What We Offer:

Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks

Suggest improvement

Next

Java JRootPane

Share your thoughts in the comments Add Your Comment

Similar Reads
Java Application with JOptionPane: Difference Between java.sql.Time,
Calculator, Numbers, Discounts, and java.sql.Timestamp and java.sql.Date in Java
Student Assessment

Skip to content
How to Convert java.sql.Date to Different Ways to Convert java.util.Date to
java.util.Date in Java? java.time.LocalDate in Java

How to Convert java.util.Date to Java AWT vs Java Swing vs Java FX


java.sql.Date in Java?

Java.io.ObjectInputStream Class in Java | Java.lang.Class class in Java | Set 1


Set 2

Java.lang.StrictMath class in Java | Set 2 Java 8 | Consumer Interface in Java with


Examples

chinmaya…

Article Tags : Geeks Premier League 2023 , Java-AWT , Geeks Premier League , Java
Practice Tags : Java

Company Explore Languages DSA Data Science & ML HTML & CSS
About Us Hack-A-Thons Python Data Structures Data Science With HTML
A-143, 9th Floor, Sovereign Corporate Legal GfG Weekly Contest Java Algorithms Python CSS
Tower, Sector-136, Noida, Uttar Pradesh - Careers DSA in JAVA/C++ C++ DSA for Beginners Data Science For Web Templates
201305
In Media Master System Design PHP Basic DSA Problems Beginner CSS Frameworks
Contact Us Master CP GoLang DSA Roadmap Machine Learning Bootstrap
Advertise with us GeeksforGeeks Videos SQL Top 100 DSA Interview Tutorial Tailwind CSS
GFG Corporate Solution Geeks Community R Language Problems ML Maths SASS
Placement Training Android Tutorial DSA Roadmap by Data Visualisation LESS
Program Tutorials Archive Sandeep Jain Tutorial Web Design
All Cheat Sheets Pandas Tutorial Django Tutorial
NumPy Tutorial
NLP Tutorial
Deep Learning Tutorial

Python Tutorial Computer Science DevOps Competitive System Design JavaScript


Python Programming Operating Systems Git Programming High Level Design JavaScript Examples
Examples Computer Network AWS Top DS or Algo for CP Low Level Design TypeScript
Python Projects Database Management Docker Top 50 Tree UML Diagrams ReactJS
Python Tkinter System Kubernetes Top 50 Graph Interview Guide NextJS
Web Scraping Software Engineering Azure Top 50 Array Design Patterns AngularJS
OpenCV Tutorial Digital Logic Design GCP Top 50 String OOAD NodeJS
Python Interview Engineering Maths DevOps Roadmap Top 50 DP System Design Lodash
Question Top 15 Websites for CP Bootcamp Web Browser
Interview Questions

Preparation Corner School Subjects Management & Free Online Tools More Tutorials GeeksforGeeks
Company-Wise Mathematics Finance Typing Test Software Development Videos
Recruitment Process Physics Management Image Editor Software Testing DSA
Resume Templates Chemistry HR Management Code Formatters Product Management Python
Aptitude Preparation Biology Finance Code Converters SAP Java
Puzzles Social Science Income Tax Currency Converter SEO - Search Engine C++
Company-Wise English Grammar Organisational Random Number Optimization Data Science
Preparation World GK Behaviour Generator Linux CS Subjects
Marketing Random Password Excel
Generator

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like