You can do this easily without running any timer, you just need to add two lines to your actionPerformed method :
public void actionPerformed(ActionEvent ae) { Object buttonClicked = ae.getSource(); if(buttonClicked.equals(theView.getStartButton())) { theModel.setGo(true);
As your Model.go () works in a separate thread, the Stream Event Manager can freely perform its actions, for example, hanging with a button down.
There is a catch! , because the Model.go () stream will work wildly! It actually called as many times per second as your system could be.
If you plan to implement some animation or something similar, you will need:
or
Example, if you go on topics:
public void go() { counter = 0; while(go) { counter++; System.out.println(counter); try { Thread.sleep(1500);
As you can see, I added Thread.sleep (1500) to 1500 in milliseconds (1.5 seconds). Thread.sleep may be interrupted for some reason, so you should catch an InterruptedException .
You don't need to handle InterruptedException deeper in this particular case, but if you're interested, you can read this nice article .
source share