Tobias H
Tobias H

Reputation: 13

Java Swing JFrame to be rendered before moving on

I want to build a "Work in progress..."-JFrame before calling a method that might take some time but the JFrame won't be fully rendered before the method is executed. I know I'm not the first with that problem, I always find solutions involving threads (do the long working method in a thread and so on). But I don't want to for particular reasons (and it might be that I'm not even able to unless I want the system to go boom).

So, is there a possibility to tell Swing to first render a window and then do the rest? Something like get rendering the window to be the first in the excecute-queue?

Upvotes: 0

Views: 499

Answers (2)

JB Nizet
JB Nizet

Reputation: 692053

You might be able to do it by displaying the JFrame and then calling

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        myLongMethod();
    }
});

But it's really a bad idea. The GUI will be frozen for the whole duration of the method.

The right way to do it is, indeed, to use a background thread. But Swing has all you need to make it painless. Simply use a SwingWorker. Its API doc is well-written and has examples. Try doing it, and if you can't, Ask another question and show the code you have tried so that someone helps you.

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

maybe How to Create a Splash Screen ???

Upvotes: 3

Related Questions