Site icon TestingDocs.com

How to make Dialog in a Java Program using JOptionPane?

Introduction

JOptionPane is used for the dialog box that prompts users for a value or informs them in a Java program. Most of the methods in the JOptionPane are of showXXDialog

For example:

showInputDialog -> To prompt user for some input.

showMessageDialog-> To tell the user about something.

More information: https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html

Code Listing

import javax.swing.JOptionPane;

public class DialogDemo {

  //***** Main method  *****//
  public static void main(String[] args) {
    String message="Hello World";
    JOptionPane.showMessageDialog(null,message);
  }
}

Taking input

A simple program to take input from the user:

import javax.swing.JOptionPane;

public class DialogDemo {

  //***** Main method  *****//
  public static void main(String[] args) {
    String input = ""; //blank
    input= JOptionPane.showInputDialog(null);
        System.out.println("You entered = "+ input);
  }
}

 

 

Exit mobile version