User (password) dialog boxes

Overview

This article shows how to create a custom dialog using JComponent and JOptionPane. In this example, we’re creating a password dialog box in which we can enter a username, password and server name.

Code

This is the code used to achieve this.

final JTextField username = new JTextField();
final JPasswordField password = new JPasswordField();
final JTextField server = new JTextField();
final JComponent[] logins = new JComponent[] {
        new JLabel("Username"),
        username,
        new JLabel("Password"),
        password,
        new JLabel("Server"),
        server
};

JOptionPane.showMessageDialog(null, logins, "My custom login", JOptionPane.PLAIN_MESSAGE);
System.out.printf("You could log on to server %s as user %s with password %s%n",
                  server.getText(),
                  username.getText(),
                  String.valueOf(password.getPassword()));

See also

The Java Tutorial How to Make Dialogs.

Advertisements
This entry was posted in java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s