
How to display popup dialog box in java?

Dialog Box- A dialog box is an independent small window which appear on your application due to some event. With the help of java swing toolkit, you can create popup dialog box. Java swing toolkit provide tow types of dialog box
Here is a simple popup message dialog box example-
import javax.swing.*;
import java.awt.event.*;
public class PopupDialogBox{
JFrame frame;
public static void main(String[] args){
PopupDialogBox dialogBox = new PopupDialogBox();
}
public PopupDialogBox(){
frame = new JFrame("Show popup Dialog Box");
JButton button = new JButton("Click to viw dialog box");
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame,"Welcome in Roseindia");
}
}
}
Description: - Here PopupDialogBox()is constructer creating a push button.On clicking your dialog box will appear.
JoptionPane class is used here for creating simple dialog box. There is a lot of way to represent a dialog box.here some of them-
ShowMessageDialog():-It is standard dialogs, contain one ok button.
You can also specify your own message, icon, title according to your need.
JoptionPane.showMessageDialog(frame,?Your message?,?title?,icon)
ShowOptionDialog()-It display defined options with specified button,message,icon,title.
You can also customize Your dialog box button text-
Object[] options={?Yes,Proceed?,?No,Go back?}
Int n=JoptionPan.showOptionDialog(frame, ?Would u like to go Delhi?? ,JoptionPan.Yes_No_OPTION,JoptionPane.QUESTION_MESSAGE,
null, //Not using custom icon
options, //button title
options[0]); //default button title
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.