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 8466969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:30:17+00:00 2026-06-10T15:30:17+00:00

I want to keep executing work while a button is pressed, using Java. When

  • 0

I want to keep executing work while a button is pressed, using Java. When the button is released, the work should stop. Something like this:

Button_is_pressed()
{
    for(int i=0;i<100;i++)
    {
        count=i;
        print "count"
    }
}

How might I achieve this?

  • 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-10T15:30:20+00:00Added an answer on June 10, 2026 at 3:30 pm

    One way:

    • Add a ChangeListener to the JButton’s ButtonModel
    • In this listener check the model’s isPressed() method and turn on or off a Swing Timer depending on its state.
    • If you want a background process, then you can execute or cancel a SwingWorker in the same way.

    An example of the former:

    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class ButtonPressedEg {
       public static void main(String[] args) {
          int timerDelay = 100;
          final Timer timer = new Timer(timerDelay , new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent e) {
                System.out.println("Button Pressed!");
             }
          });
    
          JButton button = new JButton("Press Me!");
          final ButtonModel bModel = button.getModel();
          bModel.addChangeListener(new ChangeListener() {
    
             @Override
             public void stateChanged(ChangeEvent cEvt) {
                if (bModel.isPressed() && !timer.isRunning()) {
                   timer.start();
                } else if (!bModel.isPressed() && timer.isRunning()) {
                   timer.stop();
                }
             }
          });
    
          JPanel panel = new JPanel();
          panel.add(button);
    
    
          JOptionPane.showMessageDialog(null, panel);
    
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to keep my footer at the bottom of the page while keeping
I want to keep an element always visible, even when it should be scrolled
I want a timer to keep executing in background even when the application is
I want a timer to keep executing in background even when the application is
I want my website to join some webcam recordings in FLV files (like this
I'm not sure if this is possible using regular hosting plans like those from
i want to keep my app in sync with the Server. The communication between
I want to keep my list GWT side after fetch it from service side.For
I want to keep tabs on the number of concurrent users of my application.
I want to keep a cell selected, and only change when I select another

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.