-2

I have a program here with buttons. one is a pause button and the other is a resume button. when i click on the pause button the program stops, but when i attempt to click the resume button nothing happens, i can't even click the button or even X out the window. And the click effects on the buttons also dont work. So i'm assuming the program just froze for some reason

public void createButtons() {
   fastForwardButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            synchronized(this) {
                    this.notify();
                }
            }
        });
  stopButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           synchronized(this) {
               try {
                  this.wait();
               } catch (InterruptedException e3) {
                        e3.printStackTrace();
               }
           }
      }
  });
}

public static void main(String args[]) {
   new Sim().start();
}

Any ideas would be much appreciated, thanks

Update: The purpose of this program is to have a simulation of a game and i have these 2 buttons were u can pause the game and then resume the game. but all in all, i really just want to pause the whole program and resume it

user3804711
  • 133
  • 3
  • 13
  • *`this.wait();`* What is it that needs to be stopped? Besides listening for events, what is the program actually doing at that point? *"Any ideas would be much appreciated.."* For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 18 '19 at 10:32
  • 2
    It is not clear what you want to accomplish, so I cannot even give you a solution, only the reason for the unresponsiveness. Like many UI frameworks, there is one [main thread](https://stackoverflow.com/a/13520062/5363011) in Swing that changes the UI and executes event handlers. As you have blocked that thread, it cannot do anything else. – George Leung Jul 18 '19 at 10:37
  • O sorry, i wish to pause the entire program and then resume it when i click the button – user3804711 Jul 18 '19 at 10:45
  • You cannot pause an entire program and then resume it from within that program, as it is paused. You can pause parts of the program, but you need at least something that is still able to react to the resume button. – Max Vollmer Jul 18 '19 at 10:52
  • Ah icic, that makes sense. Ill go try out some there ways then tyty – user3804711 Jul 18 '19 at 10:53
  • *i have these 2 buttons were u can pause the game and then resume the game* - you should be using a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) to schedule the animation of the game. Then to pause the game you just stop the Timer and to resume the game you resume the Timer. – camickr Jul 18 '19 at 13:55

1 Answers1

-2

Went for a small hack, i just placed a while loop in the place i wish to pause and added a isPaused state that will be true or false depending on the button pressed. The while loop will be an infinite loop till someone clicks resume, which will update the state to false and the thing will resume itself. Worked out fine in the end

user3804711
  • 133
  • 3
  • 13