Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple input in JOptionPane.showInputDialog

Is there a way to create multiple input in JOptionPane.showInputDialog instead of just one input?

like image 348
siaooo Avatar asked Jul 02 '11 04:07

siaooo


People also ask

What does JOptionPane showInputDialog return?

JOptionPane. showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise. Therefore, you can just check to see if the resultant string is null .

What does JOptionPane showInputDialog do?

This is a review of the showInputDialog() method of JOptionPane Class. With this method we can prompt the user for input while customizing our dialog window.


2 Answers

Yes. You know that you can put any Object into the Object parameter of most JOptionPane.showXXX methods, and often that Object happens to be a JPanel.

In your situation, perhaps you could use a JPanel that has several JTextFields in it:

import javax.swing.*;  public class JOptionPaneMultiInput {    public static void main(String[] args) {       JTextField xField = new JTextField(5);       JTextField yField = new JTextField(5);        JPanel myPanel = new JPanel();       myPanel.add(new JLabel("x:"));       myPanel.add(xField);       myPanel.add(Box.createHorizontalStrut(15)); // a spacer       myPanel.add(new JLabel("y:"));       myPanel.add(yField);        int result = JOptionPane.showConfirmDialog(null, myPanel,                 "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);       if (result == JOptionPane.OK_OPTION) {          System.out.println("x value: " + xField.getText());          System.out.println("y value: " + yField.getText());       }    } } 
like image 101
Hovercraft Full Of Eels Avatar answered Oct 18 '22 14:10

Hovercraft Full Of Eels


this is my solution

JTextField username = new JTextField(); JTextField password = new JPasswordField(); Object[] message = {     "Username:", username,     "Password:", password };  int option = JOptionPane.showConfirmDialog(null, message, "Login", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) {     if (username.getText().equals("h") && password.getText().equals("h")) {         System.out.println("Login successful");     } else {         System.out.println("login failed");     } } else {     System.out.println("Login canceled"); } 
like image 42
smidhonza Avatar answered Oct 18 '22 14:10

smidhonza