0

I am trying to make a program that has a toggle button (regular JButton) that when clicked, runs a while loop that runs until the button is clicked again to stop it.

I have done this, however, when I click the button, the entire JFrame freezes because of it being stuck in the while loop, as the loop will run forever until the button is pressed again. However, it is impossible to click the button again because the JFrame freezes. The button itself also just stays blue because the JFrame freezes before the colour change occurs; right before I click the button.

My code looks something like this:

   boolean isRunning=false;
   private void buttonClickEvent(ActionEvent evt) {                           
    if(isRunning){
        isRunning=false;
        System.out.println("Stopped running!");
        jButton.setText("Start Running");
    } else { // BELOW IS THE CODE THAT CAUSES IT TO LOCK
        isRunning=true;
        jButton.setText("Stop Routine");
        while(isRunning){
            // DO STUFF
        }
    }

}   

EDIT: I tried doing the following code (below) and it does print the text and allow the colour change to occur in the button, but the UI still freezes quickly afterward.

boolean isRunning=false;
   private void buttonClickEvent(ActionEvent evt) {                           
    if(isRunning){
        isRunning=false;
        System.out.println("Stopped running!");
        jButton.setText("Start Running");
    } else { // BELOW IS THE CODE THAT CAUSES IT TO LOCK
        isRunning=true;
        jButton.setText("Stop Routine");
                    new Thread(new Runnable() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        while(isInRoutine){
                            System.out.println("lolk");
                        }
                    }
                });

            }
        }).start();
    }

}   
Dan
  • 324
  • 1
  • 3
  • 13
  • 1
    You do not want the loop to be in the Event Handler, otherwise the `event` would not have been handled to completion – Scary Wombat Jul 06 '17 at 02:53
  • Where would I put it? @ScaryWombat – Dan Jul 06 '17 at 02:54
  • Probably in another thread. I can not see from this minimal code where that would be – Scary Wombat Jul 06 '17 at 02:54
  • What more would you like me to add to the code? @ScaryWombat – Dan Jul 06 '17 at 02:56
  • Also, how would I add a new thread? @ScaryWombat – Dan Jul 06 '17 at 02:56
  • 1
    A little bit here: https://stackoverflow.com/questions/19765904/how-threadpool-re-use-threads-and-how-it-works and https://stackoverflow.com/questions/790710/how-should-i-handle-multi-threading-in-java – Vasyl Lyashkevych Jul 06 '17 at 02:57
  • also look at http://stackabuse.com/how-to-use-threads-in-java-swing/ – Scary Wombat Jul 06 '17 at 02:57
  • `"Also, how would I add a new thread?"` -- ??? This suggests that you may be putting the "cart before the horse", that is trying to learn a complex corner of Java, here multi-threaded GUI programming, before learning the basics of Java and how to do object oriented programming with Java. Start at the beginning and learn the fundamentals first and you will avoid a host of frustrations. – Hovercraft Full Of Eels Jul 06 '17 at 03:04
  • I'm betting that you shouldn't even have a while loop at all, that this question is an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) in disguise and that a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) would work much better. – Hovercraft Full Of Eels Jul 06 '17 at 03:05
  • Yes, definitely use the [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). Please read the link for more on this. – Hovercraft Full Of Eels Jul 06 '17 at 03:10
  • As for why the GUI is freezing, please read [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/). – Hovercraft Full Of Eels Jul 06 '17 at 03:13
  • These are great resources, helped me fixed the problem! And you were correct, the Swing Timer had worked flawlessly for me. – Dan Jul 06 '17 at 03:42

0 Answers0