I'm implementing Consumer-Producer problem in Java and I need to add guy to this. My problem is in changing UI components from Customer
or Producer
classes.
I don't know how I can call those components from other - not related class. When I've tried to get e.g height
of component everything works like a charm but when I'm trying to set
anything, nothing happens!
This is my Producer
class code with some tries:
public class Producer extends Thread {
// Variable which holds shared queue
private BlockingQueue<String> queue;
// Amount products created by producer
private int steps;
// Object with normaln distribution
private NormalDistribution distribution;
// Accessors to the frame
private PCPMainFrame frame;
private JSlider queueSlider;
private JProgressBar queueProgressBar;
// Constructor with 4 arguments
// q - is our queue shared between customer and producer
// steps - amount of products
// mean - parameter rquired for normal distribution
// standardDeviation - ditto
public Producer(BlockingQueue<String> q, int steps, double mean, double standardDeviation){
this.queue=q;
this.steps = steps;
this.distribution = new NormalDistribution(mean, standardDeviation);
this.frame = new PCPMainFrame();
this.queueSlider = frame.getQueueSlider();
this.queueProgressBar = new JProgressBar();
}
@Override
public void run() {
// Generating products and filling queue with them
for(int i = 0; i < steps; i++){
try {
long sleepTime = Math.abs((long)distribution.sample()*100);
Thread.sleep(sleepTime);
// Saving element in queue
queue.put(String.valueOf(i));
// This is a log for developer needs, feel free to uncomment
System.out.println("Produced: " + i);
queueSlider.setValue(steps);
frame.setQueueProgressBar(queueProgressBar);
} catch (InterruptedException e) {
System.out.println("Producer exception: " + e);
}
}
// Ading exit message at the end of the queue
String exit = new String("exit");
try {
queue.put(exit);
} catch (InterruptedException e) {
System.out.println("Queue exception: " + e);
}
}
}
I have found an answer after a lot of pain!
Here is what I have done:
I've added JFrame
argument to my Producer
constructor and when I'm constructing producer in startButtonMouseClicked
I'm passing this
as an argument of JFrame
type. This litre trick has given me an access to everything just the way I want it.