0
public class Main extends JFrame{

JLabel lb1,lb2,lb3,lb4;
Button b,b1;
public Main()
{
    setLayout(null);
    Container c=getContentPane();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.red);

    setVisible(true);





    b1=new Button("CONTINUE");
    b1.setFont(new Font("Algerian", 1, 20));
    b1.setForeground(Color.black);  
    b1.setBounds(550, 480, 200,40);
    b1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ae)
      {  setVisible(false);       
           try {
            new Frame();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


      }
    });




    c.add(b1);

    setSize(1290,900);

}

public static void main(String...s) throws InterruptedException
{
    new Main();


}
}

this is the main class..and this the frame class that it calls..

public class Frame extends JFrame{


Frame() throws InterruptedException
{ 
JFrame f=new JFrame();
f.setVisible(true);
f.setLayout(null);
f.setBounds(0,0,200,200);

Container p=f.getContentPane();

p.setBackground(Color.GREEN);
f.setResizable(false);

Thread.sleep(5000);

    setVisible(false);
    new Frame1();

}

}

the main class calls the frame class..that needs to hold for a certain time and then move to another frame.. but what happens is that the main class calls it, the frame appears , but there is no content in it..nothing is displayed.then it moves to frame1()//

but if i run like

new Frame();

then it holds ,shows content , and then moves..

so why doesnt it work when Frame() is called by Main() ?

even this code doesnt' work ..instead of Thread.sleep()...this way i am not blocking any even thread..am i ?

for(double i=0;i<30;i++)
{


    for(double j=0;j<10000;j++)
    {


    }
}

new Frame1();

user2837260
  • 181
  • 3
  • 12
  • Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Nov 23 '13 at 11:46
  • .. 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 3) Don't set the size of top level containers. Instead layout the content & call `pack()`. 4) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Nov 23 '13 at 11:48
  • by freezing , u mean it will freeze with its content displayed? because that is exactly what i am aiming for.the Frame() is supposed to display a message window for 4-5 seconds and then move to Frame1()// – user2837260 Nov 23 '13 at 11:52

1 Answers1

3
  1. Swing does it's GUI rendering task inside a single thread known as Event Dispatch Thread(EDT). Any thing you do which might take a while will block the EDT and your swing application will be frozen. You are doing such thing by one of your statement:

    Thread.sleep(5000);
    
  2. This is always suggested not to work with NullLayout. Learn proper layout mangers that Swing developer has created for us with their hard work. Let us give some value to their effort.

  3. Put your GUI rendering task including frame's setVisible(true) inside EDT by using SwingUtilities.invokeLater function. For example:

    SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
               new MyWindow().setVisible(true);
            }
        });
    
  4. You are working with Multiple Frame. It is also prohibited by the Swing family of Stack Overflow. There are lots of work around shown using Card Layout by the family which you can easily adopt for avoiding use of multiple frame unnecessarily. Some example are given in the answer of Andrew Thompson with given link.

Community
  • 1
  • 1
Sage
  • 15,290
  • 3
  • 33
  • 38
  • by freezing , u mean it will freeze with its content displayed? because that is exactly what i am aiming for.the Frame() is supposed to display a message window for 4-5 seconds and then move to Frame1()// – and multiple frames?? am i supposed to do all the different work in just one frame? – user2837260 Nov 23 '13 at 11:57
  • By Freezing I mean it will hand, neither alive nor dead but won't do anything. It will show you that it is paled down by your action and hence the only way to get rid of it is to kill it from task manager – Sage Nov 23 '13 at 11:59
  • check my updated code.. i used two long loops instead of Thread.sleep() to have my message displayed.Again the same issue.. It works with call to `Frame()` , but not when it is called by `Main()` – user2837260 Nov 23 '13 at 12:03
  • This is not only about sleep. This is about any longer task you do which might try to say to EDT: "Hi wait, let me finish!". The EDT will get angry and hence stop doing things. As i have told, please get rid of multiple frame. Try to achieve same functionality using [Card Layout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). For showing message you can make use of `JDialog` or `JOptionPane`. I think you have lot to learn. But it is not something to fade up, give some time to learn the Card Layout, re-implement the thing and come back. We are always here to help you – Sage Nov 23 '13 at 12:08
  • WOO HOO..GOT TO WORK IT WITH SWING TIMER :D – user2837260 Nov 23 '13 at 12:40
  • WILL SURELY READ UP ON SWING CONCURRENCY THOUGH...AND BTW I KNOW ALL ABOUT JOPTION PANE , CARD LAYOUTS :P – user2837260 Nov 23 '13 at 12:41