1

I know that there is some questions about this but it seems like I did not understand the concept or there is something wrong with it.

Basically, what I am trying to do is, there is a algorithm which will proceed as;

for(step=0;step<value;step++){ 
//Do something;
}

But it should be an option so user can see how it perform algorithm each step if he check the isStepped checkbox; So what I came up with is

 public void run() {
for (int step = 0; step < f; step++) {
        if (isStepped) {                
            try {
                wait();//Here is the problem
            } catch (InterruptedException ex) {
                Logger.getLogger(MainThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }

And on the Menuside;

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

    MyThread.notify();

}  

So when a nextbuttonclicked, thread should wake up but it seems it wont... What causes this problem, and how can I overcome?

Ozzzy
  • 15
  • 3
  • 1
    You might consider a more MVC approach, [for example](http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749) and [example](http://stackoverflow.com/questions/29571722/java-application-with-multiple-scenes/29639672#29639672) – MadProgrammer Mar 30 '17 at 02:40

2 Answers2

3

With that for-loop you are trying to force linear console code to do event-driven processing, basically trying to shove a square peg into a round hole, and this simply isn't going to work. The proper solution is to not use threading, wait or even a for loop but rather to simplify all this and instead use a counter that increments every time the user presses the button -- change the object's state in response to an event.

For example:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;

import javax.swing.*;

public class SimpleStepProcessing extends JPanel {
    public static final String[] STEPS = { "Step One", "Step Two", "Step Three", "Step Four",
            "Step Five", "Step Six", "Step Seven", "Step Eight", "Step Nine", "Step Ten" };
    private JLabel stepLabel = new JLabel("");
    private int stepCounter = 0;
    private JButton nextButton = new JButton("Next");

    public SimpleStepProcessing() {
        nextButton.addActionListener(e -> {
            stepCounter++;
            if (stepCounter < STEPS.length) {
                // advance program to the next step
                stepLabel.setText(STEPS[stepCounter]);
            }
        });

        stepLabel.setText(STEPS[stepCounter]);

        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 4, 4));
        topPanel.add(new JLabel("Current Step:"));
        topPanel.add(stepLabel);

        JPanel middlePanel = new JPanel(new GridBagLayout());
        middlePanel.add(nextButton);

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(middlePanel, BorderLayout.CENTER);

        setPreferredSize(new Dimension(200, 100));
    }

    private static void createAndShowGui() {
        SimpleStepProcessing mainPanel = new SimpleStepProcessing();

        JFrame frame = new JFrame("SimpleStepProcessing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Wait is defined in Object not in Thread and monitoring of a thread is not very much predicted ..

To wait and notify on an object you would require a lock with synchronized statement.

To create a lock object:

private final Object lock = new Object();

To Wait on lock object:

synchronized (lock) {
                        lock.wait();
                }

To Notify on lock object:

synchronized (lock)
            {
                lock.notify();
            }

This should help to achieve your goal.

milan kumar
  • 226
  • 2
  • 16