Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Java Swing event can be used to know when application has finished starting?

Tags:

java

swing

I'm looking for a place to hook some code to programmatically create, size and position a JPanel after the application has finished loading.

I'm just starting with Java. I'm using NetBeans 6.5.1 with jdk1.6.0_13. I've used the new project wizard to create a basic Java/Swing desktop application. This is a SingleFrameApplication that uses a FrameView with a central main JPanel where all the UI elements are placed.

I first tried my code in the FrameView constructor but when I try to arrange my JPanel based on the bounding rectangle of one of the design time controls I added to the UI, that control has not yet finished being positioned and sized so I'm getting all zeros for the coordinates.

I've verified my code works as expected by calling it from a click event after the application has loaded so my problem is finding a way to know when everything is finished being sized and arranged.

I also tried the componentShown event from the main JPanel but I later read that is only fired if setVisible is explicitly called which apparently doesn't happen during normal application startup.

Can anyone provide some pointers? Thanks.

Update:

In addition to what I mention in my answer below, I also read about the Application.ready() method. This would also be a point in time of interest for knowing when the UI part of an application is finished doing everything it needs to do. Communicating to my view from the application seemed a bit messy though.

like image 564
Arnold Spence Avatar asked May 02 '09 16:05

Arnold Spence


3 Answers

The solution I went with was actually a combination of the answers from Charles Marin and JRL (I upvoted both of your answers for credit, thanks).

I had my FrameView class implement WindowListener.

...
public class MyView extends FrameView implements WindowListener
...

and in my FrameView constructor I added a listener to the application's main frame.

...
getFrame().addWindowListener((WindowListener) this);
...

Then in my implementation of windowActivated I could call the code I had to arrange and size a control on the main JPanel based on the location and size of other controls.

public void windowActivated(WindowEvent e)
{
    // The application should now be finished doing its startup stuff.
    // Position and size a control based on other UI controls here
}
like image 185
Arnold Spence Avatar answered Nov 06 '22 09:11

Arnold Spence


I think you want WindowActivated. Have a look at this part of the tutorial.

like image 23
Charlie Martin Avatar answered Nov 06 '22 09:11

Charlie Martin


I'd try using getFrame().isValid()

like image 41
JRL Avatar answered Nov 06 '22 10:11

JRL