Question

I need to create a JDialog, or a JFrame class (it doesn't particularly matter so long as it's a graphical interface) - that can be used in a static command that is called from the within the actionPerformed(ActionEvent) handler and will stall that process until the user provides their selections from the user interface.

Obviously it is possible to do, because this is exactly what the dialogs JOptionPane creates does. But when I try to stall the thread until the user has completed the selection process, the interface doesn't display properly.

Here is the part of my code that I'm having trouble with:

public static Results queryForResults(String message, int propertyOne, 
      int propertyTwo){

    MyCustomDialog mcd = new MyCustomDialog(message, propertyOne,propertyTwo);
    mcd.show();
    // complete() is a boolean property of my MyCustomDialog class that turns
    // true when the interface has been completed.
    while(!mcd.complete()){
        synchronized(mcd){
            try{
                mcd.wait(10000L);
            } catch(InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    mcd.dispose();
    // My MyCustomDialog class has a getResults() property which returns
    // a Results object (i.e. another custom class I've made to contain
    // the selections made by the user.)
    return mcd.getResults();
}

The dialog outer frame appears, but it's insides don't. They just seem to be a screen capture of whatever was on the screen beneath the dialog when it appeared. I get the impression that this isn't working because I'm not supposed to be stalling the Swing Event thread with the wait command. So how do you do it?

Was it helpful?

Solution

There is no need for a while loop. A modal JDialog will cause execution to stop in your ActionLIstener until the dialog is closed.

Also, don't use the show() method to display a dialog. You should be using the setVisible(true) method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top