Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFrame is loaded late

Tags:

java

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) {
        // TODO 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?

like image 218
sotn Avatar asked Sep 14 '11 12:09

sotn


3 Answers

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.

like image 98
Jon Skeet Avatar answered Oct 26 '22 17:10

Jon Skeet


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

like image 35
Logan Avatar answered Oct 26 '22 15:10

Logan


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,

like image 1
Anthony Accioly Avatar answered Oct 26 '22 15:10

Anthony Accioly