Search code examples
javaswingreturnjoptionpane

Return value from custom JOptionPane, Java


I'm creating a custom class that returns a JFrame, which I then pass into a JOptionPane, because I need two TextFields in the JOptionPane instead of one. Is there any way I can get a return value when OK is pressed?

 public static JFrame TwoFieldPane(){

 JPanel p = new JPanel(new GridBagLayout());
    p.setBackground(background);
    p.setBorder(new EmptyBorder(10, 10, 10, 10) );
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    p.add(new JLabel(field1), c);
    c.gridx = 0;
    c.gridy = 1;
    p.add(new JLabel(field2), c);
    //p.add(labels, BorderLayout.WEST);
    c.gridx = 1;
    c.gridy = 0;
    c.ipadx = 100;
    final JTextField username = new JTextField(pretext1);
    username.setBackground(foreground);
    username.setForeground(textcolor);
    p.add(username, c);
    c.gridx = 1;
    c.gridy = 1;
    JTextField password = new JTextField(pretext2);
    password.setBackground(foreground);
    password.setForeground(textcolor);
    p.add(password, c);
    c.gridx = 1;
    c.gridy = 2;
    c.ipadx = 0;
    JButton okay = new JButton("OK");
    okay.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            f.setVisible(false);
            //RETURN VALUE HERE
        }
    });
    p.add(okay, c);

    f.add(p);
    f.pack();
    f.setLocationRelativeTo(null);

    f.setVisible(true);
    return f;
}

And this is where I'm creating it:

try{
    JOptionPane.showInputDialog(Misc.TwoFieldPane("Server ip: ", "" , "Port: ", ""));
    }catch(IllegalArgumentException e){e.printStackTrace(); }

Solution

  • Your code is a bit unusual. Let me make suggestions:

    • Don't use a JFrame for your JOptionPane, that's a bit wacky.
    • Avoid overuse of static methods. OOPs is the way to go.
    • Create a class that creates your JOptionPane's JPanel for you and that has actual instance fields.
    • Give the class getter methods that allow you to query its state after the JOptionPane returns.
    • Create your JOptionPane and give it a JPanel created from your class above.
    • After the JOptionPane returns, query the object that you've placed in it for its field state.

    i.e., an overly simple example...

    public class MyPanel extends JPanel {
      private JTextField field1 = new JTextField(10);
      // .... other fields ? ...
    
      public MyPanel() {
         add(new JLabel("Field 1:");
         add(field1);
      }
    
      public String getField1Text() {
        return field1.getText();
      }
    
      // .... other getters for other fields
    }
    

    ... elsewhere in another class ...

    MyPanel myPanel = new MyPanel();
    int result = JOptionPane.showConfirmDialog(someComponent, myPanel);
    if (result == JOptionPane.OK_OPTION) {
      String text1 = myPanel.getField1Text(); 
      // ..... String text2 = ...... etc .....
      // .... .use the results here
    }
    

    As an aside, don't use a JTextField or Strings for passwords, not unless security isn't a concern for your application. Use a JPasswordField and char arrays instead.