Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

java - Wait non-modal window to close to resume code

I have a program that in somepoint needs to open a dialog "anDialog" which contains buttons that opens more dialogs "B" and "C", some of those child dialogs are not modal, so if I set the dialog "anDialog" as modal it raises above them right after opening and blocks them. But if I set "anDialog" as not modal, the class which calls it keeps running and it should not.

Calling dialog "A"

OpenAttributesAssistentCommand attrAssistent = new OpenAttributesAssistentCommand((InternalInterfaceAttributes) parent, transcriptor);
attrAssistent.execute();
//... more stuff after

Execute

public void execute() {
    AttributesAssistentDialog anDialog = new AttributesAssistentDialog(intFrame, transcriptor);
    anDialog.setVisible(true);
}

I want that the caller waits the dialog "anDialog" finishing before keep running. It would be nice if it could understand the difference between closing and btnOk. Also if there is a way to make it don't block non-modal childs it would be ok.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe you can do this with a SecondaryLoop, which blocks a thread without blocking the user interface until its exit method is called:

private SecondaryLoop attrAssistantLoop;

// ...

    OpenAttributesAssistentCommand attrAssistent =
        new OpenAttributesAssistentCommand(
            (InternalInterfaceAttributes) parent, transcriptor);

    attrAssistantLoop = Toolkit.getSystemEventQueue().createSecondaryLoop();

    attrAssistent.execute(attrAssistantLoop);

    attrAssistantLoop.enter();  // Wait for dialog to close

// OpenAttributesAssistentCommand class

public void execute(SecondaryLoop loop) {
    AttributesAssistentDialog anDialog = new AttributesAssistentDialog(intFrame, transcriptor);
    anDialog.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosed(WindowEvent event) {
            if (loop != null) {
                loop.exit();  // Allow waiting code to proceed
            }
        }
    });
    anDialog.setVisible(true);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...