DudiD
DudiD

Reputation: 215

JDialog in new thread doesn't respond

I want to create a JDialog in another thread (will show time), while in the main thread, a function will run..

when I create the dialog, it is shown, but it is "stuck"...and I can't see its components...

Only when the function in the main thread finish - The dialog is shown correctly..

How can I fix it?

Upvotes: 1

Views: 1114

Answers (3)

vextorspace
vextorspace

Reputation: 934

You need to use the awt event queue to open it. Just putting it on a separate thread does not work to my knowledge

must start the Face editor on another thread for the JFrame menus and accelerators to work,

    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            editFace();
        }
    });

where editFace() contains the code to start and show the new frame

            Viewer2D v = new Viewer2D(this);
            this.addFaceEditor(FaceToEdit, v);
            v.DrawFace(FaceToEdit);
            v.showAndRaise();

Upvotes: 1

StanislavL
StanislavL

Reputation: 57421

Call the JDialog creation inside SwingUtilities.invokeLater or invokeAndWait

Upvotes: 1

vaughandroid
vaughandroid

Reputation: 4374

If you're running a large task on the Swing EDT then it will block UI elements from being repainted, since that's the thread that all Swing painting happens on.

The only sensible way around this is to run your large task on a separate thread. Look into the SwingWorker class: http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html

Upvotes: 1

Related Questions