Input Dialog Box in Java Swing

An Input Dialog Box is used to accept data from the user. It appears with a component like TextField, ComboBox or List which lets the user to input the data. It is created by using the static method showInputDialog() of JOptionPane.

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class actionInputDialog extends JApplet implements ActionListener

{

JFrame jf;

JButton b1;

JLabel l1;

actionInputDialog()

{

jf=new JFrame("Input Dialog Box");

l1=new JLabel();

jf.setLayout(new FlowLayout());

b1=new JButton("Name");

b1.addActionListener(this);

jf.setSize(300,200);

jf.add(b1);

jf.add(l1);

jf.setVisible(true);

}

public void actionPerformed(ActionEvent actevt)

{

if(actevt.getSource()==b1)

{

int i=JOptionPane.QUESTION_MESSAGE;

String pstr=JOptionPane.showInputDialog(jf,"What is ur Name?","Input Dialog Box",i);

l1.setText("Ur Name is:"+pstr);

}

}

public static void main(String aa[])

{

actionInputDialog acid=new actionInputDialog();

}

}

 Input Dialog Box in Java Swing

Input Dialog Box

Explanation…

Here a button,b1 and a Label l1 are added to the frame. When b1 is clicked, an input dialog box is displayed. The name entered in the text Field of the Dialog Box is displayed on Label(l1).