Question

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);
        }
    }
}
Was it helpful?

Solution 3

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.

OTHER TIPS

In order to modify what the GUI looks like outside of the Event Dispatch Thread, you have a few options. You can use SwingUtilities.invokeLater and pass a Runnable that does your task, or use a SwingWorker.

Make sure that you actually display your frame.

Here is example code from the basic Swing tutorial:

JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);

//Display the window.
frame.pack();
frame.setVisible(true);

EDIT: Note that you are missing the bottom two lines. (If you can already see a frame, I am guessing that you are actually looking at a different frame, not the one that is generated in your code.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top