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

  • SEARCH
  • Home
  • 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 8654761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:51:56+00:00 2026-06-12T14:51:56+00:00

I am writing a program which starts a loop when start button is clicked

  • 0

I am writing a program which starts a loop when start button is clicked and I want to stop it with a stop button. Problem is when loop starts buttons do not work until loop is complete.
I’ve searched similar questions and understood the problem is loop is in actionPerformed but I couldn’t manage to take the loop outside of the actionPerformed.
I’ve tried that to make a new class and call for it etc but exactly same thing happened.
By the way I am a beginner at Java also stackoverflow and I am sorry if I did something wrong.
simply code:

OtherClass obj;
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == startButton) {
    while(true) {
        obj = new OtherClass();
        obj.doJob();
    }
    else if(e.getSource() == stopButton) {
    obj.stopLoop();
    }
}

public class OtherClass  {
    private boolean isStopped = false;
    public OtherClass() {
    }
    public void doJob(){
        while(true) {
            //loop...
            if(isStopped) {
                break;
            }
        }
    }
  • 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-12T14:51:57+00:00Added an answer on June 12, 2026 at 2:51 pm

    You need to run doJob() in a separate thread (also called a “Worker thread”). Start the thread when the button is pressed and set the isStopped flag to true when it is pressed again. Be careful not to execute UI actions in the worker thread (such as updating progress bars) – there are special methods you need to call to update the UI from the worker thread.

    Since this is a very general question (any my answer is very general too), I suggest to look out for “AWT ui thread”, “Worker thread” and “Java threading” in general on Google.

    A good starting point could be Lesson: Concurrency in Swing

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

Sidebar

Related Questions

I am writing a program that starts another java process which runs certain code.
At work training, I'm writing a Java (in which I have 0 experience) program
I am writing a program which needs to be run in Android 2.0. I
I am writing a program which needs to access data in a web server.
I m writing a program which involves converting java.util.Date to java.sql.Date... I've done it
I am writing a program which collects lots of individual pieces of data from
I'm writing a program which has both an ASP.NET configuration system and a Silverlight
I am writing a program which if I compile on a Suse 10 32-bit
I'm writing a program which will use scan conversion on triangles to fill in
I'm writing a program which will allow to load a specific managed .DLL file

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.