zupazt3
zupazt3

Reputation: 1046

Waiting for modal dialog to close before execution code in Java

I'm using Java, Swing in NetBeans and I have there main JFrame and some JDialog window.

I click on a button and JDialog is created, but all code that is after opening this JDialog is instantly executed. But I want it to execute when the JDialog is closed (JDialog is modal and it blocks main frame but not its code).

Example:

private void buttonActionPerformed(java.awt.event.ActionEvent evt)    
{                                       
    button.setText("Starting");
    About.main(null);
    button.setText("Ending");
}  

And now: After clicking button it opens JDialog and text on the button is "ending".

But I want: To click the button, it should has text "starting", then JDialog should appear and, but now, after it is closed the "ending" should be the caption.

How to do it?

Upvotes: 0

Views: 1733

Answers (1)

nachokk
nachokk

Reputation: 14413

As you said that you are using netbeans, when main method it's generated wraps object creation with SwingUtilities#invokeLater that means this code will happen after all pending AWT events have been processed. So as your System.out.println("end") is in an event your another event will execute after previous event finish!, that's why you see in console prints "end".

I don't know if it's a good design using another main method rather than your main method for whole application like a normal method.

IMO i just would use, or making an own "builder" method, cause with netbeans generation also set L&F again.

So for solving your issue just use or make your own builder method without wrapping it with invokeLater.

new MyDialog(null,true).setVisible(Boolean.TRUE); //may be not null 1st arg and a Frame or use SwingUtilities.windowForThisComponent(mycomponent);

Upvotes: 2

Related Questions