Search code examples
javaswinginputpopupjoptionpane

java double input panel


I have the following code:

String firstName;
firstName = JOptionPane.showInputDialog("First Name");

String familyName;
familyName = JOptionPane.showInputDialog("Family Name");

With this code you will have 2 boxes popping up to fill in your name. However, I would like to know if there is a way of putting those two input boxes both on one popup.


Solution

  • Just create a panel containing two text fields and then just add the panel to the dialog :

      JPanel p = new JPanel();
      JTextField familyName = new JTextField(10);
      JTextField firstName = new JTextField(10);
    
      p.add(new JLabel("Family name :"));
      p.add(familyName);
      p.add(new JLabel("First name : "));
      p.add(firstName);
    
      JOptionPane.showConfirmDialog(null, p, "Family and first name : ", JOptionPane.OK_CANCEL_OPTION);
    

    Here's what it looks like :

    enter image description here