Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:17:03+00:00 2026-06-11T18:17:03+00:00

In Java, let’s say I have a GUI with 2 buttons, Go and Pause.

  • 0

In Java, let’s say I have a GUI with 2 buttons, Go and Pause.

When I press Go, “Hello” gets printed out over and over again. When I press Pause, “Hello” no longer gets printed to the screen.

Example: User presses Go button. “Hello” gets printed out for 1 minute until the user presses “Pause.”

What is the proper way to express this approach in Java? Is it equivalent to my commented pseudocode within the goButton source?

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == goButton)
    {
        // while user has not pressed the pause button
        printHello();
    }
    else if(e.getSource() == pauseButton)
    {
        pause();
    }
}

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:17:04+00:00Added an answer on June 11, 2026 at 6:17 pm

    In order to get this to work, in reasonable fashion, you will need a Thread. This is executed in the background until such time as you decide to cancel/pause it.

    This is an EXTREMELY basic example. Normally I’d wrap the task and the GUI up in appropriate classes rather then accessing static references, but it gives a basic idea

    public class TestHello {
    
        private static HelloTask task;
    
        public static void main(String[] args) {
    
            Thread thread = new Thread((task = new HelloTask()));
            thread.setDaemon(true);
            thread.start();
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridBagLayout());
            frame.setSize(200, 200);
            frame.setLocationRelativeTo(null);
    
            JButton goButton = new JButton("Go");
            JButton stopButton = new JButton("Stop");
    
            goButton.setActionCommand("Go");
            stopButton.setActionCommand("Stop");
    
            ActionHandler handler = new ActionHandler();
    
            goButton.addActionListener(handler);
            stopButton.addActionListener(handler);
    
            frame.add(goButton);
            frame.add(stopButton);
    
            frame.setVisible(true);
    
        }
    
        public static class ActionHandler implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
                if (e.getActionCommand().equals("Go")) {
                    task.start();
                } else if (e.getActionCommand().equals("Stop")) {
                    task.pause();
                }
    
            }
    
        }
    
        public static class HelloTask implements Runnable {
    
            private static final Object WAIT_LOCK = new Object();
            private boolean dump = false;
    
            public void start() {
                synchronized (WAIT_LOCK) {
                    dump = true;
                    WAIT_LOCK.notify();
                }
            }
    
            public void pause() {
                synchronized (WAIT_LOCK) {
                    dump = false;
                    WAIT_LOCK.notify();
                }
            }
    
            @Override
            public void run() {
                while (true) {
                    while (dump) {
                        System.out.println("Hello");
                    }
                    try {
                        synchronized (WAIT_LOCK) {
                            WAIT_LOCK.wait();
                        }
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
    

    Some further read:

    • Java Concurrency
    • Concurrency in Swing

    Caveats

    NEVER try and modify the GUI from any thread other then the Event Dispatching Thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a java.util.Properties object. The Properties object has a method called
Let's say I have a method in java, which looks up a user in
Let's say I have a LazySeq of java.lang.Character like (\b \ \! \/ \b
Let's say I have a regular simple Java class, such as: public class Foo
Let's say I'm programming in Java or Python or C++ for a simple problem,
Let's say I make a Java project in Eclipse that has 3-10 classes and
Does Java guarantee array initialization? Let's say I use the code char[] uuid =
I have a java object (let's call it Foo ) with a length() method.
Let us assume a Java application, accepting an integer command line argument, say bubu
Why won't Java let me assign a value to a final variable in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.