java - How to update a ProgressBar and stop It when I want? -


in first, i'm using eclipse ide. question is: create progress bar, want make load according place program is. have refresh button, , when click entire program update. want progress bar accompanying process of updating , ends when ends.

sorry if english isn't best, i'm young portuguese developer.

my btn code

jbutton btnactualizar = new jbutton("\u21bb");     btnactualizar.addactionlistener(new actionlistener() {         public void actionperformed(actionevent arg0) {               txtipexternoloja.settext(" ");             txtssidloja.settext(" ");             txtfirewallloja.settext(" ");              txtiplojaetho.settext(" ");             txtmasklojaetho.settext(" ");             txtgwlojaetho.settext(" ");             txtdns1lojaetho.settext(" ");             txtdns2lojaetho.settext(" ");              txtiplojawlan.settext(" ");             txtmasklojawlan.settext(" ");             txtgwlojawlan.settext(" ");             txtdns1lojawlan.settext(" ");             txtdns2lojawlan.settext(" ");              txtiplojavpn.settext(" ");             txtmasklojavpn.settext(" ");             txtgwlojavpn.settext(" ");             txtdns1lojavpn.settext(" ");             txtdns2lojavpn.settext(" ");              // defaulttablemodel model = (defaulttablemodel)             // tableping.getmodel();             // model.setrowcount(0);             txttime.settext(" ");              = 0;             t.start();             btnactualizar.setenabled(false);             update();          }     }); 

my progressbar code:

jprogressbar progressbar = new jprogressbar(0, 15);     gridbagconstraints gbc_progressbar = new gridbagconstraints();     gbc_progressbar.insets = new insets(0, 0, 0, 5);     gbc_progressbar.gridx = 3;     gbc_progressbar.gridy = 0;     panelbotoes.add(progressbar, gbc_progressbar);     gridbagconstraints gbc_btnimprimir = new gridbagconstraints();     gbc_btnimprimir.insets = new insets(0, 0, 0, 5);     gbc_btnimprimir.gridx = 5;     gbc_btnimprimir.gridy = 0;     panelbotoes.add(btnimprimir, gbc_btnimprimir);     progressbar.setstringpainted(true);     progressbar.setvalue(0);      t = new timer(interval, new actionlistener() {         public void actionperformed(actionevent ae) {             if (i == 15) {                 t.stop();                 btnactualizar.setenabled(true);             } else {                 i++;                 progressbar.setvalue(i);             }         }     }); 

take @ page: https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html.

specifically, section on "using determinate progress bars" should looking for. example code little complex, simple can stuff.

take note: example uses swingworker (more on here) update progress value. because progress bar drawn in swing thread , won't updated if process running on thread too. need update program on thread not swing thread.


edit 1 - question update code

so looks you've got idea progress bar code. i've not run it, you've got looks should work. next step replace timer update() methods process. you'll need following:

  1. make sure update() method running in thread separate swing thread (something swingworker used here).
  2. from within update thread need call progressbar.setvalue(x) when want update bar, x = how far process has got.
  3. add more calls progressbar.setvalue(x) in update thread. can put these wherever like, it's best put them before , after long processes.

note: have use threads progress bars. means run things deadlock , race conditions. careful!


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -