user1897306
user1897306

Reputation: 11

Run Thread only when whole UI is up

I want to run a thrad only when whole UI is up properly ie. all component's paint() is completed. I tried with SwingUtilities.invokeLater(), but still the thread runs before whole UI is up or when all component's paint() is not complete.

Is it possible?

Thanks.

Upvotes: 1

Views: 47

Answers (2)

ATrubka
ATrubka

Reputation: 4004

OK. Now I see what your problem is. Method paint() is not an appropriate place to do your invokeLater() call.

Method paint() may be called several times. Actually, it's called whenever a portion of the screen needs to be repainted.

You may want to add your later invocation in a listener's code or upon a dialog close, but that should never be called from method paint().

Moreover, method paint() itself is always called from Event Dispatch Thread, so doing invokeLater there is pretty much redundant if you want to insure it's called from EDT.

If you show us methods, which you wrote to start up the application, we might be able to point you to where exactly you need to do your code invocation.

Upvotes: 0

Jatin
Jatin

Reputation: 31754

There is no way of knowing when the whole UI is up, primarily because you have written your own numerous components that might take their own sweet time to get loaded.

There is SwingUtilities.invokeAndWait. But the timing will be an issue. In the sense, that you precisely will have to call it once you are sure that you have called the initilization code that sets up your UI.

One way would be to probably have a checkflag. Say you have 5 main componenets, each one once loaded sets their own checkflag (or you can have say commons class which holds the flag). Your thread will keep waiting for all the ckeckflags and then proceed.

You can also use a CountdownLatch but I wont recommend it. Keep it simple.

Upvotes: 0

Related Questions