JOptionPane Lecture
JOptionPane Lecture
Introduction to JOptionPane
The JOptionPane class in Java is a part of the javax.swing package. It provides a simple way to
display standard dialog boxes for getting input, showing messages, or confirming actions in a GUI
application. This class is commonly used for creating pop-up windows that interact with users.
The main types of dialogs provided by JOptionPane are:
Syntax:
JOptionPane.showMessageDialog(Component parentComponent, Object message, String
title, int messageType);
Message Types:
• JOptionPane.PLAIN_MESSAGE – No icon.
• JOptionPane.INFORMATION_MESSAGE – Information icon.
• JOptionPane.WARNING_MESSAGE – Warning icon.
• JOptionPane.ERROR_MESSAGE – Error icon.
• JOptionPane.QUESTION_MESSAGE – Question icon.
Example:
import javax.swing.*;
Syntax:
int response = JOptionPane.showConfirmDialog(Component parentComponent, Object
message, String title, int optionType, int messageType);
Option Types:
• JOptionPane.YES_NO_OPTION – Yes and No buttons.
• JOptionPane.YES_NO_CANCEL_OPTION – Yes, No, and Cancel buttons.
• JOptionPane.OK_CANCEL_OPTION – OK and Cancel buttons.
Example:
import javax.swing.*;
Syntax:
String input = JOptionPane.showInputDialog(Component parentComponent, Object
message, String title, int messageType);
Example:
import javax.swing.*;
Syntax:
int response = JoptionPane.showOptionDialog
(
Component parentComponent,
Object message,
String title,
int optionType,
int messageType,
Icon icon,
Object[] options,
Object initialValue
);
Example:
import javax.swing.*;