Java dialogue box
In this post, you will learn about the Java dialogue box.
The dialogue box is a graphical control element in the form of a small window that communicates information to the user and prompts them for a response.
Java provides several classes to create dialogue boxes, like JOptionPane, JDialog, and JFrame. Here, we have used the JOptionPane class of Java.
JOptionPane class
The JOptionPane is a class that is used to provide standard dialogue boxes. This Java class belongs to the javax.swing package and extends the Dialog class. The dialog boxes can be of any type such as input dialog box, , message dialog box or confirm dialog box. We must include the following statement before the program's class header.
import javax.swing.JOptionPane;
JOptionPane class declaration
The JOptionPane class inherits JComponent class.
public class JOptionPane extends JComponent implements Accessible
These are the following constructors of the JOptionPane class.
- JOptionPane() - It is used to generate a JOptionPane containing a test message.
- JOptionPane(Object message) - It is used to create an instance of JOptionPane to display a message.
- JOptionPane(Object message, int messageType) - It is used to create an instance of JOptionPane to display a message with the specified message type and default options.
By using the static method of this class, we can create and customise several different kinds of dialogs. These are some static methods belonging to the JOptionPane class.
- showMessageDialog() - It is used to relay a message to the user.
- showConfirmDialog() - It is used to ask a question that requires confirmation.
- showInputDialog() - It is used to prompt a user for input.
- showOptionDialog() - It is a combination of the three other methods.
Simple JOptionPane Dialogue Box Example
import javax.swing.JOptionPane;
public class DialogBox
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "Dialogbox Example");
}
}
Output of the above code: JOptionPane Dialogue Box: showMessageDialog()
In the given Java program, we have used the showMessageDialog() method to relay a message to the user.
import javax.swing.*;
public class DialogBoxMessage {
JFrame f;
DialogBoxMessage(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Welcome to eTutorialsPoint!");
}
public static void main(String[] args) {
new DialogBoxMessage();
}
}
Output of the above code: JOptionPane Dialogue Box: showInputDialog()
In the given Java program, we have used the showInputDialog() method to prompt a user for input.
import javax.swing.*;
public class InputDialogBox {
JFrame f;
InputDialogBox(){
f=new JFrame();
String name=JOptionPane.showInputDialog(f,"Enter Name");
}
public static void main(String[] args) {
new InputDialogBox();
}
}
Output of the above code: JOptionPane Dialogue Box: showConfirmDialog()
In the given Java program, we have used the showConfirmDialog() method to ask a question that requires confirmation.
import javax.swing.*;
import java.awt.event.*;
public class DialogBoxExample extends WindowAdapter{
JFrame frm;
DialogBoxExample(){
frm=new JFrame();
frm.addWindowListener(this);
frm.setSize(300, 300);
frm.setLayout(null);
frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frm.setVisible(true);
}
public void windowClosing(WindowEvent e) {
int x = JOptionPane.showConfirmDialog(frm,"Are you sure?");
if(x == JOptionPane.YES_OPTION){
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new DialogBoxExample();
}
}
Output of the above code: JOptionPane Dialogue Box: Show error message
This class provide some static constants that we can use in parameter to show some message types, like- PLAIN_MESSAGE, ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE etc.
import javax.swing.JOptionPane;
public class DialogBox
{
public static void main( String[] args )
{
JOptionPane.showMessageDialog( null, "Something Went Wrong.." , "Error as Title",
JOptionPane.ERROR_MESSAGE );
}
}
Output of the above code: Related Articles
Enum from string JavaJava enum
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java