tags:

views:

377

answers:

4

I have 3 objects that extend JFrame let's call them FrameA FrameB FrameC.

FrameA is my main application window. From FrameA's constructor if the application is not registered i create FrameB and FrameC. They are just popup's that indicate trial period.

2 times out of 10 application freezes and never shows the B anc C frame and frame A becomes unresponsive.

I was wondering if there is a side effect of creating frames in this fashion?

A: 

Your going to want to use a JDialog. There made to do this exact thing.

Brandon
A: 

You should think about your logic. Creation of new frames in constructor of another frame sounds strange. If you doing it in the constructor - that means that first frame is not shown yet. Why don't you show you frame B and C outside of frame A constructor - logically it seems to be the same. But then depending on your conditions you can create Frame A and set it visible.

eugener
+1  A: 

That's because you are not letting FrameA finish it's construction process, you're interrupting it and then in the same thread displaying those two other frames.

I would suggest to change your strategy and use a factory method and probably in conjunction with SwingUtilities.invokeLater method.

Let say you have something like:

 public static void main( String [] args ) {
      JFrame a = new FrameA(); // Has both display logic and trial "pop up" logic
 }

Change it for:

 public static void main( String [] args ) {
      JKFrame a = FrameA.createFrame(); // the same but splited like the following code 
 }


 class FrameA extends JFrame {
      // new method
      public static JFrame createFrame() {
            return new FrameA();  

      }
 }

Nothing changed you just added the factory method, later you can:

         public static JFrame createFrame() {
            if( thatSpecialLogicSaysItIsTrialAndShouldPopUp() ) {
                  JFrame b = new FrameB();
                  b.setVisible( true );
                  JFrame c = new FrameC();
                  c.setVisible( true );
                  return new FrameA();

           } else {
            return new FrameA();
           }  

      }

What you will be doing in the second code is separate the logic from the class constructor and put it in the factory method. That way if thatSpecialLogicSaysItIsTrialAndShouldPopUp returns true, you display B and C

You can also as described use a JDialog there, but pretty much the problems will be gone when you separate the responsabilities.

OscarRyz
A: 

The symptoms you describe sounds like something's locking up the event thread - this may or may not have anything to do with how you're launching the frames - you'll want to run in a debugger and when it starts exhibiting the symptoms check on the state of the event thread.

(Though I would agree with everyone else in the thread that you should pull the frame launching logic to some other place - however - there's nothing "unsafe" about calling frame constructors from another frame constructor - they're no different from any other constructor. It may be bad design - but it shouldn't lock up the event thread.)

Nate