wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JDialog return value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog return value" Watch "JDialog return value" New topic
Author

JDialog return value

Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Hi,

I would like to show a JDialog subclass modally, and when the dialog closes I would like it to return a Boolean status. Apart from passing a Boolean into the constructor which is then set by one of the dialog's operations is there any other way to achieve this?

Thanks,
Dan


SCJP, SCJD, SCWCD
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Dan Murphy:
I would like to show a JDialog subclass modally, and when the dialog closes I would like it to return a Boolean status. Apart from passing a Boolean into the constructor which is then set by one of the dialog's operations is there any other way to achieve this?


From how you phrase this, can we assume that the JOptionPane.showConfirmDialog() approach is unsuitable?


bitguru blog
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Yes, the class is not a JOptionPane it's my own subclass of JDialog which contains various custom input components.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Well unless you want to disable the JOptionPane buttons, you can use any object inside JOptionPane - even complete customized components:

If you want to have custom labels on the buttons you can use the "showOptionDialog" method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Brian Cole:
can we assume that the JOptionPane.showConfirmDialog() approach is unsuitable?


What I meant by this was if you take a look at the source code for
JOptionPane.showConfirmDialog() [the bulk of which is actually in
JOptionPane.showOptionDialog()], couldn't you do something similar?

Just create your custom subclass of JDialog (which I assume is modal)
instead of the JDialog JOptionPane.showOptionDialog() cooks up.
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
I didn't know you could completely replace the body of a JOptionPane - thanks for the tip! However, that doesn't really solve my problem, though is the problem is my poor description thereof. Allow me to explain.....

What I really want to happen is:

1. Main window pops up a JDialog which is passed a data object in constructor
2. User modifies the data object's fields in the JDialog
3. User clicks save and JDialog attempts to save data in DB
4. Main window updates table if save was successful

My problem is how to pass the result (success/fail) of the save operation from the JDialog to the Main app window (in your JOptionPane example your just indicating which button was clicked).

One option is to pass a Boolean to the JDialog's constructor, which is what I'm doing with the data object, but I think this is a bit ugly.

Thanks Again,
Dan
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Dan Murphy:
What I really want to happen is:

1. Main window pops up a JDialog which is passed a data object in constructor
2. User modifies the data object's fields in the JDialog
3. User clicks save and JDialog attempts to save data in DB
4. Main window updates table if save was successful

My problem is how to pass the result (success/fail) of the save operation from the JDialog to the Main app window (in your JOptionPane example your just indicating which button was clicked).


Does saving to the database happen before or after the model dialog is dismissed?

If after, a typical way to do this is for your subclass of JDialog to have something like a wasSuccessful() method. The code could look something like this:
MyCustomDialog d = new MyCustomDialog(...);
d.setVisible();
if (d.waswasSuccessful()) { /* update main window */ }

If before, then I guess I would use a callback--have the save-to-db code call a method when it's done.


One option is to pass a Boolean to the JDialog's constructor, which is what I'm doing with the data object, but I think this is a bit ugly.


Booleans are immutable, so I'm not sure what you're getting at here.
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Booleans are immutable, so I'm not sure what you're getting at here.
doh
[ October 29, 2007: Message edited by: Dan Murphy ]
john whelan
Greenhorn

Joined: May 07, 2009
Posts: 1
Hello... Long time reader, first time poster here.

Keep in mind that the dialog object is still around after it is no longer visible. I use code like the following when launching a dialog; it works for me:

JFrame mainFrame = Mp3CollectionManagerApp.getApplication().getMainFrame();
PickCollectionDirectoryDialog dirDialog = new PickCollectionDirectoryDialog(mainFrame, true);
dirDialog.setLocationRelativeTo(mainFrame);
Mp3CollectionManagerApp.getApplication().show(dirDialog);
String startingDir = dirDialog.getStartingDir();
Logger.getLogger(Mp3CollectionManagerView.class.getName()).log(Level.INFO, ("startingDir set to '" + startingDir + "'"));

The 'getStartingDir()' method is one that I added to get a resultant value back after the dialog's OK button is selected. (At which time I hide the dialog.)

I hope this is helpful.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

It would have been more useful if this thread wasn't a year and a half old. Also, what are these Mp3CollectionManagerApp and PickCollectionDirectoryDialog classes? Without their code (although I can guess what these calls do) your code is not really clear.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JDialog return value
 
Similar Threads
JDialog question
implementing a component like mouse over element in eclipse
closing a JDialog box
MODAL JFRAME
Casting a null object...