14 Replies - 3245 Views - Last Post: 17 February 2012 - 11:56 AM Rate Topic: -----

#1 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Simple progress bar

Posted 17 February 2012 - 09:39 AM

Sorry for all the posts guys, I promise this'll be my last one for ages!

I've never used a progress bar EVER. I just think it would be a nice addition to my plugin.

Is it possible to have a bar in indeterminate mode where, when a button is pressed, done is set to false and the bar animates until the action is complete at which point done = true?

So something like

private void simulateButtonActionPerformed(java.awt.event.ActionEvent evt) {
boolean done=false;
progressBar.(ANIMATES WHEN done==false);
... (complete actions);
done = true;
progressBar.(STOPS ANIMATION WHEN done == true);



Is This A Good Question/Topic? 0
  • +

Replies To: Simple progress bar

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 09:41 AM

I would totally use JProgressBar instead.

Check out that link and if you have any specific questions feel free to ask.

This post has been edited by Fuzzyness: 17 February 2012 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 10:16 AM

View PostFuzzyness, on 17 February 2012 - 09:41 AM, said:

I would totally use JProgressBar instead.

Check out that link and if you have any specific questions feel free to ask.


Thank you, that was really helpful!

Any idea why the progress bar does not reset to 0 when I click the simulate button a 2nd time?

Here is the (incomplete!) code:

private void simulateButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    //initialise progress bar
    progressBar.setIndeterminate(true);
    int bar = 0;
    progressBar.setValue(bar);
...

try {
...
        //get user input number of nodes and iterations
        int noNodes = Integer.parseInt(nodesBox.getText());
        int noIters = Integer.parseInt(iterBox.getText());

//set progress bar max
            progressBar.setMaximum((noNodes*noIters));
            progressBar.setIndeterminate(false);

//action loop here
for (int c = 0; c < noNodes; c++) {
                for (int i = 0; i < noIters; i++) {
...

//increase progress on bar
                    bar = bar + noIters;
                    progressBar.setValue(bar);
}
}
}catch{naughtiness}
}

Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 10:22 AM

Hmm.. what is it doing when you hit it a second time?
Was This Post Helpful? 0
  • +
  • -

#5 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 10:29 AM

View PostFuzzyness, on 17 February 2012 - 10:22 AM, said:

Hmm.. what is it doing when you hit it a second time?


Well the first time it becomes a full bar of little blue bars (not exactly in steps like I was hoping, but it'll do) however if I hit the button again, even though it is working the bar is still full and doesn't change.
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 10:36 AM

Well it does it in steps like a progress but you have no delay so it goes as fast as it is computed.. Which is MiliSeconds.

Try adding a delay to it. Add it after you update the progress of the bar. I believe it is working just goes so fast.

Thread.sleep(1000);

That is in miliseconds so 1000 ms is 1 second.
Was This Post Helpful? 0
  • +
  • -

#7 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 10:46 AM

View PostFuzzyness, on 17 February 2012 - 10:36 AM, said:

Thread.sleep(1000);


Sorry to be so thick, but where should I add this?
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 10:48 AM

"Add it after you update the progress of the bar."

So in the nested for loop after you call setValue(bar) to update it, call that method. This will give it a second between iterations so you can see progress and we will know if your JProgress bar really isn't changing and what not. Debug method :^:
Was This Post Helpful? 1
  • +
  • -

#9 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 10:54 AM

Now I get errors and squiggly red lines:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.InterruptedException; must be caught or declared to be thrown

But if I run anyway the bar gets stuck at 25% so I think it is working.
Was This Post Helpful? 0
  • +
  • -

#10 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 11:09 AM

Got rid of errors with
//increase progress on bar
                    bar = bar + noIters;
                    progressBar.setValue(bar);
                    if (Thread.interrupted()) {
                        throw new InterruptedException();
                    }
                    Thread.sleep(500);


but now when I run it it crashes horribly and without error! My bar is dooooomed!

Ah, called sleep inside a loop. D'oh.

All good now, still stuck on the full blue bar at the end but I'm not too bothered, just gonna leave it alone I think :P thank you so much for your help!
Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 11:20 AM

Sure thing, what did you want the bar to be at the end? If it is 100% it is done.
Was This Post Helpful? 0
  • +
  • -

#12 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 11:32 AM

View PostFuzzyness, on 17 February 2012 - 11:20 AM, said:

Sure thing, what did you want the bar to be at the end? If it is 100% it is done.


Yes its up to 100% again :) its just annoying that when I click Simulate again it doesn't go back to 0%, but I'm mostly doing this for my own amusement so it's not a massive problem :P
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 11:38 AM

Well if it still isn't resetting lets figure it out.

Try initializing it in the ActionListener.

progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
int bar = 0;
progressBar.setValue(bar);



Maybe recreating an initialization of it will work. I don't see why it wouldn't reset to 0, you clearly tell it to.
Was This Post Helpful? 0
  • +
  • -

#14 CantCodeWontCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-February 12

Re: Simple progress bar

Posted 17 February 2012 - 11:54 AM

That didn't seem to change anything. Maybe it does start at 0 but its just so fast it loads to 100% again too quick for me to see? :)
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Simple progress bar

Posted 17 February 2012 - 11:56 AM

Well that is why you put the Sleep(1000), which is 1 second, if the max is 100 would take 100 seconds, clearly visible. So I do not think that that is the case.

Work on full complete code and post it. I will copy paste it into my IDE and see if I cannot get it to work. Will be later though at work without an IDE >_<

This post has been edited by Fuzzyness: 17 February 2012 - 11:57 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1