1

I am trying to display a Loading Image in a new JFrame when the User clicks a particular button in my application.The JFrame is displayed,but it shows nothing!,also with a WHITE background,whereas all the JFrames have a grey default background.What is Wrong here?

stop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            analyzer.running=false;
            JFrame Load1=new JFrame("Load1");
            ImageIcon icon1=new ImageIcon("./ajax-loader.gif");
            System.out.println(icon1.getIconHeight());
            Load1.add(new JLabel("Retrieving...", icon1, JLabel.CENTER),BorderLayout.CENTER);
            Load1.pack();
            Load1.setSize(400,400);
            Load1.setVisible(true);

            System.out.println("Start Processing");
            parser.parse();  // Time Consuming method


            nw_Creator.create();
            System.out.println("End Processing");
            Load1.setVisible(false);

            home.setVisible(false);
            screen2.setVisible(true);

        }
    });
nikel
  • 3,402
  • 11
  • 45
  • 71
  • I don't see icon1 being added to the JFrame in question? – vaisakh Mar 29 '12 at 18:05
  • Sorry for that,i just pasted the relevant code.Missed that line by mistake. – nikel Mar 29 '12 at 18:10
  • This isn't the answer to your question, but I notice you are calling both pack() and setSize(). Calling both of those methods is inappropriate. Call either pack() or setSize() but not both. The pack() method will set a size based on the contents. There are rare occasions when the size of the content can't be determine, in that case you would remove the call to pack() and call setSize() explicitly. – Michael Mar 29 '12 at 20:55

2 Answers2

4

Do not put time consuming parts in an event handler or any method running in the event dispatch thread. You may want to use a swing worker instead.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • Could you tell me what's happening over here?The JFrame is drawn before the CPU intensive task,so it should be displayed,Right? – nikel Mar 29 '12 at 18:06
  • 2
    @nikel Unfortunately event dispatching is a quite complex task. Although your code "looks right" and the methods are in the right order, the handling of drawing the frame and firing actions is done in a very different way. Methods like `setVisible` or other are themselves creating events and queuing them into the event queue. Only after one event (in this case the action listener handling) is completed the next one will be started. In short words the JFrame is *not* drawn before the CPU intensive task but afterwards. – Howard Mar 29 '12 at 18:10
  • +1 for `SwingWorker`. See also this related [example](http://stackoverflow.com/a/4530659/230513). – trashgod Mar 29 '12 at 19:48
1

What is happening is that you are never releasing the UI thread, so your JFrame is never painted. Since all graphics operations are done on the UI thread, you must release it, do your calculations, then close the frame if you want the jframe to display anything.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80