0

I want to show a progress bar. I put the code to open the JFrame containing progress bar at the beginning of the function. But the progress bar opens only after the complete execution of the function

public void function(){
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ProgressSearch().setVisible(true);
        }
    });

    instruction 1;
    instruction 2;
    instruction 3;
    instruction 4;
    instruction 5;
}

this code above only displays the progress bar after completing the function

Dan
  • 7,286
  • 6
  • 49
  • 114
Tibin Tomy
  • 29
  • 6
  • 1
    For better help sooner, post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) or a [Short, Self Contained, Correct Example](http://www.sscce.org/) – Dan Dec 29 '16 at 09:01
  • Also, try removing the invoke later and just have `new ProgressSearch().setVisible(true);` instead of `java.awt.EventQueue.invokeLater(new Runnable() {...}` – Dan Dec 29 '16 at 09:03
  • I TRIED THAT ONLY FRAME IS VISIBLE. NO PROGRESS BAR – Tibin Tomy Dec 29 '16 at 09:13
  • 1
    Okay please don't shout. It was only a suggestion. As I said post a MCVE or a SSSCE for better help – Dan Dec 29 '16 at 09:14
  • 1
    Yeah, that will not work. Just put the instructions in the `windowOpened` event of `ProgressSearch` – AhmadWabbi Dec 29 '16 at 09:15
  • 1
    1) As @Dan already suggested: post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). **Voting to close for lack of one.** 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 3) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Dec 29 '16 at 11:43

1 Answers1

0

Try adding static "progress fields" to your class. Then just update those fields as your JFrame is created, and get their values on your code using Class.StaticField

e.g.,

public class MyClass {
    public static PROGRESS;
    public MyClass () {...}
    (...)
    PROGRESS = (...);
    (...)
    PROGRESS = (...);
    (...)
}

and get MyClass.PROGRESS

Also, don't forget to reset PROGRESS variable after the full completion so that every time you create another JFrame from the same class you get a clean progress from 0.

Regards,

Dan
  • 7,286
  • 6
  • 49
  • 114
Wildmind
  • 13
  • 8