Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Need help with progress bars

  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with progress bars

    For some reason my 1st progress bar will not show what is running in the background. The second one is the only one that runs. Here is my code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Lab4Part2 extends JFrame{
    	//attributes
       JProgressBar jpBar;
       JLabel prg;
       JProgressBar jpB = new JProgressBar();
       JProgressBar jp2 = new JProgressBar();
       //int w = 0;
       boolean keepGoing = true;
    	
       public Lab4Part2(){
          setTitle("Lab 4 Part 2");
          setLocationByPlatform(true);
          setLayout(new GridLayout(0,1));
       	
          JButton start = new JButton("Let's start the show");   		
       	
          JLabel prg1 = new JLabel("Progress #1");
          JLabel prg2 = new JLabel("Progress #2");
       	
          final InnerProgress ip1 = new InnerProgress(prg1, jpB);
          final InnerProgress ip2 = new InnerProgress(prg2, jp2);
       	
          add(start);
          add(ip1);
          add(ip2);
          
          start.addActionListener(
                new ActionListener(){
                   public void actionPerformed(ActionEvent ae){
                      Thread t1 = new Thread(ip1);
                      t1.start();
                      Thread t2 = new Thread(ip2);
                      t2.start();
                   }
                });
    
       
          
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          setVisible(true);
          
          
       }
    		
       class InnerProgress extends JPanel implements Runnable{
          public InnerProgress(JLabel _prg, JProgressBar _jpBar){
             jpBar = _jpBar;
             prg = _prg;
             add(prg);
             add(jpBar);
          					
             jpBar.setMinimum(0);
             jpBar.setMaximum(80);
          	jpBar.setIndeterminate(true);
             jpBar.setStringPainted( true );
          					
          }
          public void run(){
          jpBar.setIndeterminate(false);
          for(int i = 0;i <=80 && keepGoing == true; i++){
             jpBar.setValue(i);
             try{
              Thread.sleep((int) Math.random()*100);
             }
             catch(InterruptedException ie){
                ie.printStackTrace();
             }	
             System.out.println("We are running");
             System.out.println("Finishing Millisecond Value:"+System.currentTimeMillis());
          }
          keepGoing = false;
         }
       }
    	
       public static void main(String[] args){
          Lab4Part2 labp2 = new Lab4Part2();
       }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,940
    Rep Power
    4

    Default Re: Need help with progress bars

    1. You need to put ()'s around Math.random*100. Otherwise, you cast Math.random to int which means zero.
    2. Your are simply reassigning the same variable jpBar in your inner class (it is defined in the enclosing class) You need to move both jpBar and prg inside InnerProgress (or just use the parameters _jpBar and _prg directly)
    3. You don't need the keepGoing flag unless you want the first progress bar to complete to stop the whole process.


    Regards,
    Jim
    Last edited by jim829; 09-20-2013 at 07:02 PM.
    ndsmith20 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with progress bars

    Thank you so much you have resolved the issue Jim. I do need it to stop when one bar has reached the end. Now I'm trying to get it to declare the winner and then allow me to restart the threads. I know i need to use the Thread.interrupted() method but have not been able to figure out where to place. If you could assist again that would be greatly appreciated otherwise i understand. Thanks again.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with progress bars

    Also, you could have a look at the SwingWorker class; it takes care of all that Thread crapola.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Progress Bar showing no progress
    By igorland in forum New To Java
    Replies: 8
    Last Post: 05-03-2013, 05:39 PM
  2. Replies: 0
    Last Post: 03-01-2012, 12:21 PM
  3. Help with Scroll Bars
    By Abion47 in forum AWT / Swing
    Replies: 5
    Last Post: 03-02-2011, 04:44 AM
  4. Progress Bars - How do they work?
    By Unsub in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •