Retrieve input entered in a JDialog

Posted by Jules Olléon on Stack Overflow See other posts from Stack Overflow or by Jules Olléon
Published on 2010-04-12T07:19:15Z Indexed on 2010/04/12 7:23 UTC
Read the original article Hit count: 483

Filed under:
|
|
|
|

I extended JDialog to create a custom dialog where the user must fill some fields : dialog

How should I retrieve the data entered ?

I came up with a solution that works. It mimics JOptionPane but the way I do it looks ugly to me because of the static fields involved... Here is roughly my code :

public class FObjectDialog extends JDialog implements ActionListener {
    private static String name;
    private static String text;
    private JTextField fName;
    private JTextArea fText;
    private JButton bAdd;
    private JButton bCancel;

    private FObjectDialog(Frame parentFrame) {
        super(parentFrame,"Add an object",true);
        // build the whole dialog
        buildNewObjectDialog(); 
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource()==bAdd){
            name=fName.getText();
            text=fText.getText();
        }
        else {
            name=null;
            text=null;
        }
        setVisible(false);
        dispose();
    }

    public static String[] showCreateDialog(Frame parentFrame){
        new FObjectDialog(parentFrame);
        String[] res={name,text};
        if((name==null)||(text==null))
            res=null;
        return res;
    }
}

As I said, that works properly, but I guess that might raise serious concurrency issues...

Is there a cleaner way to do that ? How is it done in JOptionPane ?

© Stack Overflow or respective owner

Related posts about java

Related posts about gui