开发者

JFrame is loaded late

开发者 https://www.devze.com 2023-04-05 00:13 出处:网络
My code is: solveDb_userfileInconsistency solve = new solveDb_userfileInconsistency(); solve.setVisible(true);

My code is:

    solveDb_userfileInconsistency solve = new solveDb_userfileInconsistency();
    solve.setVisible(true);

    try {
        solve.solveIt();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // T开发者_如何学运维ODO Auto-generated catch block
        e.printStackTrace();
    }

"solveIt" method returns after 30 seconds and until it returns, frame isn't installed properly but after solveIt method returns, the frame gets installed properly but what i want is that before going into solveIt method, the frame should be properly on the screen. Is there any method that can wait the frame's installation and then calls that solveIt method?


It sounds like you're probably doing all of this on the UI thread. Don't do that - make solveIt execute on a background thread, calling into the UI thread using SwingUtilities if it needs to update/query the UI. Basically, you shouldn't do significant work in the UI thread - see the Swing concurrency tutorial for more information.


Take a look at SwingWorker Class. It is used to do the background processes without stopping the frame to be installed.


Use the event-dispatching thread for short lived GUI related code. Long running tasks should be execute in their own threads as proposed in the other answers.

Since people bet me to it. Let me just complement their answers with some interesting links:

  • Lesson: Concurrency in Swing
  • Threads and Swing
  • Using a Swing Worker Thread

Note that the two links points to somewhat old but relevant documentation of JFC. Nowadays SwingWorker is included in the Standard API.

Cheers,

0

精彩评论

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