Java Swing Tutorials

Show Dialog Box in Java
previous Home Next

Input dialog box is very useful and interactive feature of Java Swing. this control is using in System.in for inputting anything from user. Java Swing provides the facility to input any thing in a normal window means that the Input Dialog Box allow to input any type of data in run time of the programs. The input dialog box contains two buttons, first is the "Ok" button and another is the "Cancel" button.

Example

package r4r;
import javax.swing.*;
import java.awt.event.*;
public class showinputdialogbox {
public static void main(String[] args){
JFrame frame = new JFrame("Input Dialog Box Frame");
JButton button = new JButton("Show Input Dialog Box");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String str = JOptionPane.showInputDialog(null, "Enter some text : ", "R4RTech Soft Solution", 1);
if(str != null)
JOptionPane.showMessageDialog(null, "You entered the text : " + str, "R4RTech Soft Solution", 1);
else
JOptionPane.showMessageDialog(null, "You pressed cancel button.", "R4RTech Soft Solution", 1);
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
previous Home Next