0

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.

msrd0
  • 7,816
  • 9
  • 47
  • 82
Arian.No
  • 3
  • 1
  • 5
  • 1
    And it's still extremely unclear. If you know how to deal with 1 progress bar, then do the same thing 3 times, and you'll know how to deal with 3 progress bars. I don't see any swing code in your question. – JB Nizet Nov 23 '14 at 09:38
  • When I add other progress bars, I don't know how to update to separate ones from different threads, they all end up updating to the same bar, and the way I've tried it opens up a new window for each progressbar. I'm not sure how to put them all in the same window. – Arian.No Nov 23 '14 at 09:42
  • We can't tell you how to improve or fix your code without seeing your code. Post the relevant code. – JB Nizet Nov 23 '14 at 09:43
  • Sorry, I'm really new to this stuff so what I've got so far is just things I've found by searching for answers. I have a progress bar that doesn't receive updates yet, I wanted to understand what I'm doing before doing anything. And I have added my code to the bottom. – Arian.No Nov 23 '14 at 10:02
  • Post your code here, in your question. Not on pastebin. Remove the irrelevant code. – JB Nizet Nov 23 '14 at 10:04
  • Alright, the formatting took a bit, but I got it there...it's not that impressive of code sorry. – Arian.No Nov 23 '14 at 10:14
  • You want 3 progress bars, yetyou only create and add one to your frame. And I don't see anywhere where you start your threads, and how the threads update the progress bars. – JB Nizet Nov 23 '14 at 10:26
  • My threads start in the main. Where it says xRobThread.start(); and the run method is under the Robot class. I just updated the progress bar part, but that's as far as my knowledge takes me. I wrote a comment where I'm confused. Also my progress update which is in my run method isn't changing progress from 0. – Arian.No Nov 23 '14 at 10:39
  • 2
    Either you create three setProgress() methods, and each thread calls the one it wants, or you pass an additional argument to tell whcih progress bar you want to update. – JB Nizet Nov 23 '14 at 10:44
  • So there's only one run method. How do I call out the separate threads to set their own progress? Is there something that identifies the current thread like 'this' does for object? – Arian.No Nov 23 '14 at 10:55
  • 1
    Create three different [`SwingWorker`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html) class instead of your three robor threads. You have to add `PropertyChangeListeners` to each task where you can set the proper `JProgressBar`'s progress depending on the task's `process` property. – Bence Kaulics Nov 23 '14 at 11:09
  • Well I am required to use threads for the assignment so I don't know if that's allowed unless SwingWorker class counters a thread. – Arian.No Nov 23 '14 at 11:33
  • A `SwingWorker` object uses one or more threads to perform tasks. See [SwingWorker](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html) for more information. – Freek de Bruijn Nov 23 '14 at 14:37

0 Answers0