Roshan Prabhakar
Roshan Prabhakar

Reputation: 174

JOptionPane with multiple inputs

I need to get input in different ways using JOptionPane. Specifically, I need a dropdown menu along with the default input text field to both be present in the same JOptionPane. Is this achievable? if so, how?

Upvotes: 0

Views: 1862

Answers (1)

kAliert
kAliert

Reputation: 768

If you need different components in your pane, you can try to implement something like this:

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 result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
    System.out.println("You entered " +
            firstName.getText() + ", " +
            lastName.getText() + ", " +
            password.getText());
} else {
    System.out.println("User canceled / closed the dialog, result = " + result);
}

Upvotes: 1

Related Questions