Java: How to have a definite JProgressBar to load things? -


ok understand how indefinite jprogressbars don't understand how definite ones. example i'm using jprogressbar indicate loading game. in loading game have information want load in information files , initialize variables:

private void load() {     myint = 5;     mydouble = 1.2;     //initialize other variables } 

and have 4 files want load information from. how translate give accurate loading bar out of 100%?

you need work in separate thread , update progress bar's model work progresses.

it important perform model updates in swing event thread, can accomplished executing update code swingutilities.invokelater.

for example:

public class progress {     public static void main(string[] args) {         jframe frame = new jframe("loading...");         frame.setdefaultcloseoperation(jframe.exit_on_close);          jprogressbar progressbar = new jprogressbar(0, 100);         frame.add(progressbar);         frame.pack();         frame.setvisible(true);          // callback function updates progress in swing event thread         consumer<integer> progresscallback = percentcomplete -> swingutilities.invokelater(             () -> progressbar.setvalue(percentcomplete)         );          // work in thread         completablefuture.runasync(() -> load(progresscallback));     }      // example function "work" , reports progress caller     private static void load(consumer<integer> progresscallback) {         try {             (int = 0; <= 100; i++) {                 thread.sleep(100);                 // update progress bar percentage completion                 progresscallback.accept(i);             }         } catch (interruptedexception e) {             // ignore         }     } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -