File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Getting response from a dialog box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting response from a dialog box" Watch "Getting response from a dialog box" New topic

Getting response from a dialog box

Mick Flynn

Joined: May 09, 2001
Posts: 8
I created a dialog box class called YNDialog by subclassing JDialog. It has two buttons, (predictably enough) labelled Yes and No.
Question: how do I get the YNDialog object to tell the rest of my application which button was clicked?
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Depend on what you want to do. If you just need a standard Yes and No dialog, you can us the JOptionPane.
But if you really want to implement your own, then your dialog should handle the Events for the button and probably wrap them up and provide a method like addXXXListener() for your main application. And have your application implement the Listener interface.
Good luck,
[This message has been edited by Ryo Saeba (edited June 18, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

In your subclassed YNDialog, you can have either a single boolean, or one for each answer (makes checking with if's in the client application a bit easier)... and in the actionEvent for the Yes button, you'd set YNDialog.YES = true, and YNDialog.NO = false, or a single variable YNDialog.ANSWER = true

then once the dialog has disappeared your client application would examine the variables and determine which button had been clicked. If encapsulation appeals to you, you can make it private variables and a method, probably

boolean YNDialog.getAnswer() //(true for Yes, false for No).
Mick Flynn

Joined: May 09, 2001
Posts: 8
Mike, Ryo,
Thanks a million for that - in fact JOptionPane was what I've been looking for for weeks... I thought the only option was to subclass JDialog.
Muchas gracias!
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Don't you find that getting to KNOW all the stuff that is out there in the API, and understanding where it would be useful, it a major part of the battle. I know I run into things ALL THE TIME that I wish I had known for something or other earlier.
Ah Well, I just keep plowing on.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Getting response from a dialog box
It's not a secret anymore!