开发者

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开发者_JS百科 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?


maybe How to Create a Splash Screen ???


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜