title: Pause button java
body: <p>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.</p>
  
  <pre><code>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 &lt; 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");
           }
        });
  
     }
  }
  </code></pre>