I have created a custom dialog box which requires 3 fields
public class Test{
public static void main(String args[])
{
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
new JLabel("First"),
firstName,
new JLabel("Last"),
lastName,
new JLabel("Password"),
password
};
JOptionPane.showMessageDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE);
System.out.println("You entered " +firstName.getText() + ", " +lastName.getText() + ", " +password.getText());
}
}
How do I check if the user has inserted all fields or not? And even if user close the dialog box it display
You entered , ,
I want to check user input in field and close application if user close dialog box without dsplaying
"You entered , , "
You can check if user has inserted all fields or not as follows:
if(firstName.getText() != "" && lastName.getText() != "" && password.getText() != "")
System.out.println("All fields have been filled!");
else
System.out.println("Some fields are need to be filled!");
EDIT:
For displaying a message after closing the dialog box, you can do it like:
myframe.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(null,"You entered " + firstName.getText() + ", " + lastName.getText() + ", " +password.getText());
}
});
EDIT2:
OK, I think I understand your question now, try this:
public class Test
{
public static void main(String args[])
{
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[]
{
new JLabel("First"),
firstName,
new JLabel("Last"),
lastName,
new JLabel("Password"),
password
};
int i = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE);
if(i == 0) System.out.println("You entered " + firstName.getText() + ", " + lastName.getText() + ", " + password.getText());
}
}