JoptionPane Showinputdialog With Password

The needs is that we need have a dialog to ask user user name and password so user are to be granted appropriate privilege to access resource, we will use JoptionPane Showinputdialog but with password textbox presented.

How JoptionPane showinputdialog dialog are construed?

Name: <jtextfield>
Password: <JPasswordField>
Button <OK> Button <NO>

Source code example 1 that implements a password textarea within joptionpane showinputdialog:

public static void main(String[] args) {
        JLabel jUserName = new JLabel("User Name");
        JTextField userName = new JTextField();
        JLabel jPassword = new JLabel("Password");
        JTextField password = new JPasswordField();
        Object[] ob = {jUserName, userName, jPassword, password};
        int result = JOptionPane.showConfirmDialog(null, ob, "Please input password for JOptionPane showConfirmDialog", JOptionPane.OK_CANCEL_OPTION);

        if (result == JOptionPane.OK_OPTION) {
            String userNameValue = userName.getText();
            String passwordValue = password.getText();
            //Here is some validation code
        }
}

We created a JPasswordField added it to confirm dialogs as a component. Follow the same way we can create a register dialog.

Example 2 show password textarea in JoptionPane showinputdialog

 JLabel jUserName = new JLabel("User Name");
 JTextField userName = new JTextField();
 JLabel jPassword = new JLabel("Password");
 JTextField password = new JPasswordField();
 JLabel jSex = new JLabel("Sex");

 String[] item = new String[]{"Male", "Female"};
 JComboBox box = new JComboBox(item);
 JLabel jEmail = new JLabel("Email");
 JTextField email = new JPasswordField();
 Object[] ob = {jUserName, userName, jPassword, password, jSex, box, jEmail, email};
 int result = JOptionPane.showConfirmDialog(null, ob, "Please input password for JOptionPane showConfirmDialog", JOptionPane.OK_CANCEL_OPTION);

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *