ghassen92
ghassen92

Reputation: 305

Pause button java

I want to set "pause" button in order to pause my thread if I clicked in the first time .If I hit the button another time the thread should be resumed .I set a boolean "marche" it should controls my thread every time I changed it.

import java.awt.event.*;
import javax.swing.*;

public class PauseButton {
   static int count = 0;
   static boolean marche = true;
   static Thread mythread;
   static JButton button = new JButton("Pause");

   public static void main(String[] args) {
      JFrame fen = new JFrame("EDT");
      fen.getContentPane().add(button);
      fen.setSize(200, 100);
      fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fen.setLocationRelativeTo(null);
      fen.setVisible(true);
      updateBouton();
      setButtonLis();
   }

   public static void updateBouton() {
      mythread = new Thread() {
         public void run() {
            for (int i = 0; i < 100; i++) {
               while (marche) {
                  try {
                     Thread.sleep(1000);
                  } catch (InterruptedException e) {
                     e.printStackTrace();
                  }
                  button.setText("Pause " + ++count);
               }
            }
         }
      };

      mythread.start();

   }

   static public void setButtonLis() {

      button.addMouseListener(new MouseListener() {

         @Override
         public void mouseClicked(MouseEvent arg0) {

            if (marche)
               marche = false;
            else
               marche = true;
            new Thread() {
               public void run() {
                  System.out.print(marche + " ");
               }
            }.start();
         }

         @Override
         public void mouseEntered(MouseEvent arg0) {
         }

         @Override
         public void mouseExited(MouseEvent arg0) {
         }

         @Override
         public void mousePressed(MouseEvent arg0) {
         }

         @Override
         public void mouseReleased(MouseEvent arg0) {
            // button.setText("Pause");
         }
      });

   }
}

Upvotes: 0

Views: 2264

Answers (2)

PooBucket
PooBucket

Reputation: 63

maybe this is what you're looking for. public class Start {

    public static WhateverThread thread;
    public static boolean isPaused = false;

    public static void main(String...strings){
        thread = new WhateverThread();
        thread.start(); 
        JFrame frame = new JFrame("pausebutton test");

        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);

        JPanel content = new JPanel();

        content.setSize(frame.getSize());
        content.setLayout(null);

        JButton pause = new JButton("Pause");

        pause.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isPaused) {
                    thread.resumeThread();
                    System.out.println("Thread runs");
                } else {
                    try {
                        thread.pauseThread();
                        System.out.println("Thread waits");
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

                isPaused = !isPaused;
            }

        });
        content.add(pause);
        pause.setLocation(10, 10);
        pause.setSize(100, 20);
        frame.add(content);
        frame.setVisible(true);
    }

    static class WhateverThread extends Thread{

        private boolean isRunning = true;

        @Override
        public void run() {
            System.out.print("I am running...");
        }

        public void pauseThread() throws InterruptedException
        {
            isRunning = false;
        }

        public void resumeThread()
        {
            isRunning = true;
        }
    }
}

The classes and variables are only made static because this is a quick and dirty snippet and I wanted to call everything from within the main Method.

Upvotes: 0

ELITE
ELITE

Reputation: 5940

change your updateBouton code to the following...

public static void updateBouton() {
    th = new Thread() {
        public void run() {
//            for (int i = 0; i < 100; i++) {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(marche) {
                        bouton.setText("Pause " + ++count);
                    }
                }
//            }
        }
    };
    th.start();
}

when you click on button then variable marche was false so the while loop in thread closes and thread completed its execution so make a infinite while loop and check boolean to update text on button..also there is no need for the for(int i=0;i<100;i++) loop..

Upvotes: 1

Related Questions