0

I've been reading about progress bars, and there are some pieces of the puzzle I can't really understand. I don't understand how to set a progress bar that shows the status of a given thread or a calling to a method that calls other methods.

The code below is a quick recap of what basically I'm doing at the moment.As you can see, I'm just showing a simple and basic pop up with a trivial message "processing information" and once the called methods are done , another pop up "Operation Complete!"... I know it's a bad approach especially because it doesn't provide any feedback to the user of what is going on.

  Thread t = null;
 try {
 if (...) {
  t = new Thread(new Runnable() {
    public void run() {
   JOptionPane.showMessageDialog(null, "processing information ....", "Wait",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            });

            t.start();
        }

        if (response == null)
         waitResponse();


        if (...) {
                        //do some controls
             {
                if (...)
                    throw new AssertionError("you can't edit that field..");
            }

        }
        //
        if (type.equals("A"))
            methodA(1234);
        else if (type.equals("B"))
            methodB(1234);
        else if (type.equals("C")) {
            methodC(1234);
        } else if (type.equals("D")
            methodD(1234);   

 if (...) {
t = new Thread(new Runnable() {
    public void run() {
                    JOptionPane.showMessageDialog(null, "Operation Complete!", "Done",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            });
            t.start();

}

I would to understand/learning how can I use a progress bar to show the status of the program. I've read some information about doInBackground() , publish(int i) , process(List chunks) but I don't understand how to use them.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Maxuel
  • 127
  • 10
  • Swing IS single thread and NOT thread safe - start with [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html). *"I've read some information about doInBackground() , publish(int i) , process(List chunks) but I don't understand how to use them"* - Then you're going to have to learn, as this is the right way to do it – MadProgrammer Jan 30 '19 at 23:25
  • [For example](https://stackoverflow.com/questions/15199091/progress-bar-java/15199220#15199220) – MadProgrammer Jan 30 '19 at 23:26
  • Showing progress which is generated from sub methods is all about providing a means for those methods to provide the feedback you need. I'd consider starting with a `interface` that provides a simple `setProgress` method, which would be passed down to those methods to use, this all requires some fore-thought into the design, which, oddly enough, is demonstrated [here](https://stackoverflow.com/questions/54012205/jprogressbar-from-oracle/54012823#54012823) – MadProgrammer Jan 30 '19 at 23:27
  • @MadProgrammer thank you for the information above! Just check out the last example , pretty clear about the mean idea behind . Now , there is only one thing missing to connect the all the pieces in the pluzze : as far as I understood after defining a progressable interface , declaring , making a reference to it (in the class? : The one that calls n methods to perform the given task ) and creating a swingworker class I should calculate in some way how much time has gone performing each method? e.g if (type.equals"A") methodA(1234); int progress = ...// You mean calculate in some – Maxuel Jan 30 '19 at 23:53
  • Yes, basically, you need to "know" how much needs to be done and be able to report the change in that the progression – MadProgrammer Jan 31 '19 at 00:14

0 Answers0