How to open dialog box in java
This Java tutorial will focus on how to open dialog box in Java. We will learn how to create show input dialog box. Then we will learn how to create show message dialog box and finally how to create confirm dialog box.
Create Dialog Box using JOptionPane in Java
using JOptionPane in java we can create the dialog box. The types of the Dialog box is three.
- Show input dialog box
- Show Message Dialog box
- Confirm Dialog box
I know all of you are excited so let’s start
If you did not read the last post please be read first. In this last post, we have already discussed the Action Listener.
If we perform the operation after clicking the button we have must need of action listener.
How to create Show Input Dialog box
JOptionPane.showInputDialog
The method of the java Dialog box is (JOptionPane.showInputDialog) by the help of this method we can create the dialog box for input.

input dialog box in Java
How to Create Show Message Dialog Box
JOptionPane.showMessageDialog
The method of the java Dialog box is (JOptionPane.showMessageDialog) by the help of this method we create the message dialog box.

message dialog box in Java
Confirm Dialog Box
JOptionPane.showConfirmDialog
The method of the java Dialog box is (JOptionPane.showConfirmDialog) by the help of this method we can create a dialog box for the confirmation.

confirm dialog box in Java
Code of Dialog Box
import javax.swing.*; import java.awt.event.*; import java.awt.*; class Dialog implements ActionListener { JFrame fr; JButton b1,b2,b3; Dialog() { fr=new JFrame(); fr.setLayout(null); fr.setSize(600,600); b1=new JButton("Message"); b1.setBounds(250,200,100,30); fr.add(b1); b2=new JButton("Sumbit"); b2.setBounds(250,250,100,30); fr.add(b2); b3=new JButton("Input"); b3=new JButton("Input"); b3.setBounds(250,300,100,30); fr.add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); fr.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { JOptionPane.showMessageDialog(fr,"This is message Dialog"); } if(e.getSource()==b2) { int x=JOptionPane.showConfirmDialog(fr,"Swing is EXT of AWT","Query",JOptionPane.YES_NO_CANCEL_OPTION); if(x==JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(fr,"Right Answer"); if(x==JOptionPane.NO_OPTION) JOptionPane.showMessageDialog(fr,"WRONG Answer"); if(x==JOptionPane.CANCEL_OPTION) JOptionPane.showMessageDialog(fr,"Ans to dena Padega"); } if(e.getSource()==b3) { String name=JOptionPane.showInputDialog(fr,"enter your name","ABCD"); if(name!=null) JOptionPane.showMessageDialog(fr,"Welcome"+name); else JOptionPane.showMessageDialog(fr,"Welcome Unknown"); } } public static void main(String s[]) { new Dialog(); } }
In this code, we have created 3 dialog boxes.
- Message Dialog Box
- Confirm Dialog Box
- Input Dialog Box.
The output for the program will be similar to the above screenshots already given.
Leave a Reply