Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve input entered in a JDialog

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 ?

like image 257
Jules Olléon Avatar asked Apr 12 '10 07:04

Jules Olléon


People also ask

What is modal in JDialog?

JDialog(Dialog owner, boolean modal) Creates a dialog box with the specified Dialog owner and modality. JDialog(Dialog owner, String title) Creates a modeless dialog box with the specified Dialog owner and title.

What is JDialog in Java?

JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need . Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified owner.

Where is JDialog used in an application?

JDialog is one of the important features of JAVA Swing contributing to interactive desktop-based applications. This is used as a top-level container on which multiple lightweight JAVA swing components can be placed to form a window based application.


2 Answers

If I do this, I always works like this:

FObjectDialog fod = new FObjectDialog(this);
fod.setLocationRelativeTo(this); // A model doesn't set its location automatically relative to its parent  
fod.setVisible(true);
// Now this code doesn't continue until the dialog is closed again.
// So the next code will be executed when it is closed and the data is filled in.
String name = fod.getName();
String text = fod.getText();
// getName() and getText() are just two simple getters (you still have to make) for the two fields their content
// So return textField.getText();

Hope this helps!
PS: Your program looks great!

like image 158
Martijn Courteaux Avatar answered Oct 16 '22 11:10

Martijn Courteaux


If you intend to display multiple dialogs at the same time, then you have concurrency issues, not otherwise. However, getting rid of all the static stuff would make the design cleaner, safer and easier to test. Just control the creation and showing of the dialog from the calling code and you don't need any static stuff.

like image 21
fish Avatar answered Oct 16 '22 12:10

fish