CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > Dialog Box in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel
  • Home

Dialog Box in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

The dialog box in Java Swing is a graphical user interface element used to display messages, gather user input, or prompt users to make decisions. Swing provides several types of dialog boxes to interact with users, such as message dialogs, input dialogs, and option dialogs.

Table of Contents

  • Message Dialog
    • Info Message
    • Error Message
    • Warning Message
    • Plain Message
    • Custom Icon Message
  • Option Dialog

Following is an example of how we can create and use different types of dialog boxes in Java Swing:

Message Dialog

A message dialog is used to display informative messages to the user.

Info Message

JOptionPane.showMessageDialog(null, "This is a message dialog.", "Info message", JOptionPane.INFORMATION_MESSAGE);

Output:

info message in joption pane swing

Error Message

JOptionPane.showMessageDialog(null, "This is a message dialog.", "Error message", JOptionPane.ERROR_MESSAGE);

Output:

error message in joption pane swing

Warning Message

JOptionPane.showMessageDialog(null, "This is a message dialog.", "Warning message", JOptionPane.WARNING_MESSAGE);

Output:

warning message in joption pane swing

Plain Message

JOptionPane.showMessageDialog(null, "This is a message dialog.", "Plain message", JOptionPane.PLAIN_MESSAGE);

Output:

plain message in joption pane swing

Custom Icon Message

Display custom Icon from URL:

try {
	ImageIcon customIcon = new ImageIcon(
					new URL("https://example.com/myicon.png"));
	JOptionPane.showMessageDialog(null, "This is a message dialog.", "Custom Icon message",
	JOptionPane.PLAIN_MESSAGE, customIcon);
} catch (MalformedURLException e) {
	e.printStackTrace();
}

Display custom icon from a local file path:

ImageIcon customIcon = new ImageIcon("C:\\Users\\codersathi\\Pictures\\mylogo.png");
JOptionPane.showMessageDialog(null, "This is a message dialog.", "Custom Icon message",
					JOptionPane.PLAIN_MESSAGE, customIcon);

Output:

custom icon in joption pane swing

Option Dialog

An option dialog displays a message along with multiple options for the user to choose from. For example, let’s see the following code and output:

Object[] options = { "Physics", "Biology", "Mathematics", "Computer Science", "Humanities" };
		int n = JOptionPane.showOptionDialog(null, "Please select your option from the list below.",
				"Which subject do you study?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
				options, options[2]);
// The following line prints the index value from the options when user clicks on that option
		System.out.println(n);

Output:

option dialog with custom button in joption swing java

When you click on this button, it will return the output value as an index of options array starting from 0 (zero). If you click on the X (close) icon then it will print -1.

Related Posts:

  • Control Statements in Java
  • List in Java
  • MySQL Commands for Developers
  • Interface in Java: Mastering Abstraction and…
  • Compile and Run Java Program
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
Tags:guijavaswing
Was this article helpful?
← Previous ArticleJPasswordField in Java Swing
Next Article →What is JDBC: Bridging Java and Databases

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap