I extended JDialog to create a custom dialog where the user must fill some fields : dialog http://www.freeimagehosting.net/uploads/3d4c15ed9a.jpg
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 ?
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.
If I do this, I always works like this:
Hope this helps!
PS: Your program looks great!