Search code examples
javaswingjoptionpane

Several input fields with JOptionPane?


I wonder if it's possible to have several input fields i the JOptionPane in Java, instead of one like in the code below?

String info = JOptionPane.showInputDialog("Name?");

Solution

  • Something like this...

    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class OptionPaneTest {
    
    public static void main(String[] args) {
        JPanel myPanel = new JPanel();
        JTextField field1 = new JTextField(10);
        JTextField field2 = new JTextField(10);
        myPanel.add(field1);
        myPanel.add(field2);
        JOptionPane.showMessageDialog(null, myPanel);
        System.out.println(field1.getText() + field2.getText());
    }
    }