5

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?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • 1
    Is this what you want ? http://stackoverflow.com/questions/1719166/making-a-jbutton-stay-depressed-manually – Anuj Kulkarni Sep 01 '12 at 05:09
  • 1
    I can't say this is a good idea, mostly because of the way the Event Queue works. If you block when the button press occurs, you will never receive a mouse up event... – MadProgrammer Sep 01 '12 at 05:12
  • No What i want is Whnever user keep pressing the button then some process should start..and when user leave that pressed button then that should be stop – BhavikKama Sep 01 '12 at 05:23
  • 2
    (I would) Use a `JToggleButton` (for this). – Andrew Thompson Sep 01 '12 at 05:24

4 Answers4

10

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);

   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

I want to keep executing work while a button is pressed

Execute that process in another thread and then your form is not block and you can press the button to cancel or stop the execution.

see :

Community
  • 1
  • 1
Mehdi
  • 4,396
  • 4
  • 29
  • 30
1

You may need to use mousePressed event to start the action

And use mouseReleased event to stop the action (This is neccesary)

For more information refer here

Arjun Sunil Kumar
  • 1,781
  • 3
  • 28
  • 46
  • 1
    No, I urge you not to use a MouseListener for this. For one, the MouseListener will still work even if you try to prevent it from working by disabling the JButton. Best to avoid use of MouseListeners with JButtons in general. – Hovercraft Full Of Eels Sep 01 '12 at 05:25
  • 1
    @Hov I'd have +1'd for just the last sentence. The rest of the comment was excellent advice as well. – Andrew Thompson Sep 01 '12 at 05:26
0

For Android Apps

I know this question is old, but you can use:

while (yourButton.isPressed()) {

    // Do Stuff
}
Sid110307
  • 497
  • 2
  • 8
  • 22