Updating a set number of progress bars in one JFrame(preferably)

373 views Asked by At

Looked around and there were a few similar questions, but none seemed to show how to run more than one progress bar in a single JFrame while updating it from 3 other threads.

Preferably I plan to have a progressbar class of its own, a lot of the examples I saw people doing all the progressbar work inside a main thread which I didn't really like. If that's how you're meant to do it I'm sorry I've never used a progressbar before.

I want to monitor the progress of the robot threads. I was going to send an update directly from the Robot class to my progress bar and have a progressbar object in my main if that's possible.

my main thread is sort of like this

CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
Motor m = new Motor();

Robot xRob = new Robot(cyclicBarrier, m);
Robot yRob = new Robot(cyclicBarrier, m);
Robot zRob = new Robot(cyclicBarrier, m);

Thread xRobThread = new Thread(xRob);
Thread yRobThread = new Thread(new Robot(cyclicBarrier, m));
Thread zRobThread = new Thread(zRob);

boolean clockwise = true, counterClockwise = false;




m.setMotor(clockwise, 14400000, xRob);
m.setMotor(clockwise, 0, yRob);
m.setMotor(counterClockwise, 36000000, zRob);

xRobThread.start();
yRobThread.start();
zRobThread.start();

This is the important parts of my robot class.

public class Robot implements Runnable{
public void run(){
    System.out.println("Running: ");
    m.Engage(this);
    try {
        System.out.println("Sleeping: ");
        Thread.sleep(3000);
        cyclicBarrier.await();

    } catch (Exception e){
        e.printStackTrace();
    }
    System.out.println("Engaging: ");
}

public void Rotate(){
        if ((opcode & clockwise) > 0){
            rotation++;
            if(rotation == 360){
                rotation = 0;
                moveCount++;
            }
        }
        if ((opcode & counter) > 0){
            rotation--;
            if(rotation == -360){
                rotation = 0;
                moveCount --;
            }
        }

}
}

Here is the progress bar I have so far

public class ProgressBar {


final int MAX = 100;
final JFrame frame = new JFrame("JProgress ");
final JProgressBar pbOne = new JProgressBar();
final JProgressBar pbTwo = new JProgressBar();
final JProgressBar pbThree = new JProgressBar();

    ProgressBar(){ 

        pbOne.setMinimum(0);
        pbOne.setMaximum(MAX);
        pbOne.setStringPainted(true);

        pbTwo.setMinimum(0);
        pbTwo.setMaximum(MAX);
        pbTwo.setStringPainted(true);

        pbThree.setMinimum(0);
        pbThree.setMaximum(MAX);
        pbThree.setStringPainted(true);

        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(pbOne);
        frame.getContentPane().add(pbTwo);
        frame.getContentPane().add(pbThree);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }


    public void setProgress(int progress){
        try {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    pb.setValue(progress); //HERE IS where I get lost. How do I differentiate between PBs and Threads
                                           // As in which thread the code is coming from and which progress bar it's updating.
                }
            });
            java.lang.Thread.sleep(100);
        } catch (InterruptedException e) {
        JOptionPane.showMessageDialog(frame, e.getMessage());
        }
    }
}

I was going to update the progress everytime it went through a rotation in my robot class, but I haven't implemented that yet since I wanted to first get the design down.

0

There are 0 answers