Generating dialog boxes in Java

In this tutorial, we will create a code that will help us to generate dialog boxes in Java. In Java, we will use JOptionPane class to generate dialog boxes. 

JOptionPane is a built-in class with which we can generate dialog boxes. We can use dialog boxes in different ways, such as displaying error messages, taking input in the dialog box, or confirming some message like yes or no. There are different methods in JOptionPane class with which we can generate different types of dialog boxes.  In Java, swing class is used for creating window-based applications. You can learn more about it from the link.

JOptionPane class methods

There are three methods to generate dialog boxes:

1. JOptionPane.showMessageDialog():- This method is used to generate a message dialog box where we can display any message.

2. JOptionPane.showInputDialog() :- This method is used to generate input dialog box where we can take input from user .

3. JOptionPane.show confirm dialog():- This method is used to generate confirm dialog box where we can take confirmation from the user. 

 Let’s create the code.

 

import javax.swing.JOptionPane;
class DailogBox
{
   public static void mai(String...args)
   {
      JOptionPane.showMessageDialog(null,"Hello World");
    }
}

Note: If you are using this code in the online compiler, it won’t work so make sure that you have an IDE like Netbeans or eclipse.

Output:

The above code will generate a message dialog box that will display hello world.

 We have import the JOptionPane class from the subpackage swing from the package javax and also used null in the parentheses because there is no graphical window at present.

Leave a Reply

Your email address will not be published. Required fields are marked *