What Java Swing event can I use to find out when the application was finished?

I am looking for a place to select code for programmatically creating, the size and position of JPanel after the application has finished loading.

I am just starting with Java. I am using NetBeans 6.5.1 with jdk1.6.0_13. I used the new project wizard to create the main Java / Swing application. This is a SingleFrameApplication that uses a FrameView with a central core JPanel where all user interface elements are hosted.

At first I tried my code in the FrameView constructor, but when I try to arrange my JPanel based on the bounding box of one of the design time controls that I added to the user interface, this control has not finished positioning and size yet, so I'm getting everything zeros for coordinates.

I checked that my code works as expected, triggering it from the click event after the application has loaded, so my problem is to find out when it was all over, size and location.

I also tried the componentShown event from the main JPanel, but later I read that it only fires if setVisible is explicitly called, which apparently does not happen when the application starts normally.

Can someone point out some pointers? Thanks.

Update:

In addition to what I mention in my answer below, I also read about the Application.ready () method. It will also be a point of interest to find out when part of the user interface of the application is completed, doing whatever it takes. Communication with my view from the app seemed a bit dirty though.

+6
java swing
source share
4 answers

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

I got the FrameView WindowListener class.

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

and in my FrameView constructor, I added a listener to the main frame of the application.

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

Then, in my windowActivated implementation, I could call the code I needed to organize and the size of the control on the main JPanel, based on the location and size of the 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 } 
+4
source share

I think you want WindowActivated. Check out this part of the tutorial .

+3
source share

I would try using getFrame().isValid()

+1
source share

I assume this is a WYSIWYG editor. I am watching NetBeans 6.1, so your impressions may vary.

The traditional way to build Swing components is to use LayoutManager (or LayoutManager2 ). According to NetBeans help, the visual editor supports them if they do not require support for restrictions.

The procedure looks something like this:

  • Create a new JavaBean and implement LayoutManager (BeanInfo is also required to support the palette), you can create it by right-clicking the bean class)
  • Build a project
  • Right-click on the bean and select "Tools"> Add to palette ... and add it
  • Right-click the panel for which you want to set the layout, and choose Install Layout> Your bean name

You may find that development time is not enough.

Layout implementation example:

 public class StepLayoutBean extends Object implements Serializable, LayoutManager { public void addLayoutComponent(String name, Component component) { } public void layoutContainer(Container container) { Dimension space = container.getSize(); int xoffset = 0; int yoffset = 0; for (Component kid : container.getComponents()) { Dimension prefSize = kid.getPreferredSize(); if (prefSize.width + xoffset > space.width) { xoffset = 0; } Rectangle bounds = new Rectangle(xoffset, yoffset, prefSize.width, prefSize.height); kid.setBounds(bounds); xoffset += prefSize.width; yoffset += prefSize.height; } } public Dimension minimumLayoutSize(Container container) { Dimension size = new Dimension(); for (Component kid : container.getComponents()) { Dimension minSize = kid.getMinimumSize(); size.width = minSize.width > size.width ? minSize.width : size.width; size.height += minSize.height; } return size; } public Dimension preferredLayoutSize(Container container) { Dimension size = new Dimension(); for (Component kid : container.getComponents()) { Dimension prefSize = kid.getPreferredSize(); size.width += prefSize.width; size.height += prefSize.height; } return size; } public void removeLayoutComponent(Component component) { } } 

If the custom layout does not match the count, look at the event bindings in the component properties panel, although resizing this path can be a recipe for a recursive storm of events.

+1
source share

All Articles