Java Swing Tutorials

Show Dialog Box in Java
previous Home Next

In the Swing the Message dialog box is used to display messages to the user. In this section we are using JOptionPane class to display the message Dialog box. Our program display "Click On Me" button on the window and when user clicks on it program displays Message box with "OK" button and message "R4RTech Soft Solution".

Code Description

  1. showMessageDialog(): This method is used to display a simple message.
  2. showInputDialog(): This method is used to display a prompt for inputting and returns a String value which is entered by user.
  3. showConfirmDialog(): This method is used to asks the user for confirmation (Yes/No) by displaying message.

Example

package r4r;
import javax.swing.*;
import java.awt.event.*;
public class showdialogbox {
JFrame frame;
public static void main(String[] args){
showdialogbox sdb = new showdialogbox();
}
public showdialogbox(){
frame = new JFrame("Show Message Dialog");
JButton button = new JButton("Click on Me");
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame,"R4RTech Soft Solution");
}
}
}
previous Home Next