showMessageDialog Displays a modal dialog with a button labeled “OK”. It can be easily specify the message, icon, and title that will display the dialogue.
A message dialog box displays plain messages to the user and is created by using the static method showMessageDialog () of JOptionPane. The general form of showMessageDialog () method is:
public static void showMessageDialog(Component parent, Object message)
where,
parent is the parent frame to which dialog box is attached
message is the message to be displayed
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE = JavaExampleDialogMessageInJApplet.class WIDTH = 370 HEIGHT = 300 ></APPLET>*/
public class JavaExampleDialogMessageInJApplet extends JApplet implements ActionListener
{
JButton BtnInfo = new JButton("Show Information Dialog");
JButton BtnErr = new JButton("Show Error Dialog");
JButton BtnWarn = new JButton("Show Warning Dialog");
JButton BtnQues = new JButton("Show Question Dialog");
JButton BtnPln = new JButton("Show Plain Dialog");
public void init()
{
Container Cntnr = getContentPane();
Cntnr.setLayout(new FlowLayout());
Cntnr.add(BtnInfo);
Cntnr.add(BtnErr);
Cntnr.add(BtnWarn);
Cntnr.add(BtnQues);
Cntnr.add(BtnPln);
BtnInfo.addActionListener(this);
BtnErr.addActionListener(this);
BtnWarn.addActionListener(this);
BtnQues.addActionListener(this);
BtnPln.addActionListener(this);
}
public void actionPerformed(ActionEvent e1)
{
String DlgTitle = "Alert Dialogs";
String DlgMsg = "Hi from Java!";
int DlgType = JOptionPane.PLAIN_MESSAGE;
if(e1.getSource() == BtnInfo)
{
DlgType=JOptionPane.INFORMATION_MESSAGE;
}
else if(e1.getSource() == BtnErr)
{
DlgType = JOptionPane.ERROR_MESSAGE;
}
else if(e1.getSource() == BtnWarn)
{
DlgType = JOptionPane.WARNING_MESSAGE;
}
else if(e1.getSource() == BtnQues)
{
DlgType = JOptionPane.QUESTION_MESSAGE;
}
else if(e1.getSource() == BtnPln)
{
DlgType = JOptionPane.PLAIN_MESSAGE;
}
JOptionPane.showMessageDialog((Component) null,DlgMsg,DlgTitle,DlgType);
}
}