JOptionPane Closing Question

I am trying to close a dialog window when a boolean variable in another class changes to false. I use JOptionPane inside of an instance of Dialog. I have tried using hide(), dispose(), and setVisible(false). The problem is that the dialog window does not know to check the variable. Do I need to add some kind of event listener or what? I know this isn't compilable but I think this is a simple question, I must just be overlooking something...

Object[] options ={"Stop"};

JOptionPane optionPane =new JOptionPane("Playing...",

JOptionPane.OK_OPTION,

JOptionPane.INFORMATION_MESSAGE,

dialogIcon,//don't use a custom Icon

options,//the titles of buttons

options[0]);//default button title

Dialog playingDialog = optionPane.createDialog(frame,"Status");

playingDialog.setVisible(true);

//here is what needs to happen when the orhb class sets getPlayState to false

if(!orhb.getPlayState()){

System.out.println("done playing, close window");

setVisible(false);

}

[1524 byte] By [walden_rainesa] at [2007-11-27 1:13:32]
# 1
how about 'playingDialog.setVisible(false); ?
abillconsla at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 2

I am not sure what orhb is. But here are several options:

1. Have the option pane poll the state of orhb and close when the state if false

2. Setup the option pane as a listener to "orhb" and have orhb notify it's listeners during a state change.

3. Have orhb close the dialog.

The specifics of your code are going to determine what the best option is.

zadoka at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 3

Here is the code that provoked my answer, above:

playingDialog.setVisible(true); // Note that setVisible is called on the playingDialog

//here is what needs to happen when the orhb class sets getPlayState to false

if(!orhb.getPlayState()){

System.out.println("done playing, close window");

setVisible(false); // Note that setVisible is NOT called on the playingDialog

abillconsla at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 4

> Here is the code that provoked my answer, above:

Yea, I thought the same thing at first but then I when I re-read it, I realized that the OP was saying this code:

> //here is what needs to happen when the orhb

> class sets getPlayState to false

> if(!orhb.getPlayState()){

>System.out.println("done playing, close window");

> setVisible(false);

is not being called and the question was where to put it. (Of course once he determines where to put that code he will have to call it on the appropriate object)

zadoka at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 5
How would you do option 1?
walden_rainesa at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 6
> How would you do option 1?You would have to set a timer and periodically check the state. Although for what I understand of your situation this would not be optimal.
zadoka at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 7
Basically the problem is that the program does not go to the next line of code when the dialog is open. If I try to check the condition with a while statement it does not work until after the box is closed. I don't know how to check the condition while the dialog is open.
walden_rainesa at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...
# 8
W/o seeing more code, it sounds like an event Thread problem. Have you read - or are you otherwise aware of these considerations this [url http://java.sun.com/developer/JDCTechTips/2005/tt0419.html#1] T-Tip[/url] discusses?
abillconsla at 2007-7-11 23:48:54 > top of Java-index,Java Essentials,Java Programming...