• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDialog return value

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the class is not a JOptionPane it's my own subclass of JDialog which contains various custom input components.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic