How can combine threads and buttons?

How can combine threads and buttons?

I would like to start an application, stop it and restart it again and stop etc. I tried the following code. It does start and stop, but I get an error when I try to restart.

Any suggestions?

  /*  * To change this template, choose
  Tools | Templates  * and open the
  template in the editor.  */

  package testactionevent;

import javax.swing.*; import
  java.awt.<em>; import java.awt.event.</em>;

 /**  *  * @author */ public
  class TestActionEvent extends JFrame
  implements ActionListener{
      private JButton jbtStart = new JButton("Start");
      private JButton jbtStop = new JButton("Stop");
      Thread print100 = new Thread(new SiThread());

public TestActionEvent(){

setTitle("TestActionEvent");

getContentPane().setLayout(new FlowLayout());
getContentPane().add(jbtStart);
getContentPane().add(jbtStop);

jbtStart.addActionListener(this);
jbtStop.addActionListener(this); }
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    TestActionEvent frame = new TestActionEvent();
    frame.setTitle("Two buttons");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 100);
    frame.setVisible(true);


}

public void actionPerformed(ActionEvent e){

    if (e.getSource() == jbtStart){
       System.out.println("Start ");

       String a = new Boolean(Thread.currentThread().isInterrupted()).toString();
       System.out.println("Start " + a);
       if (!print100.isInterrupted()){
           System.out.println("Startbbbbbbb ");
        print100.start();
       }
    }
    else if (e.getSource() == jbtStop){

        print100.interrupt();
    }
} }
 class SiThread implements Runnable {


public void run() {

    int num = 1;

    System.out.println(" - " + num);

    while (0 == 0){
        num = num +1;
        System.out.println(" " + num);
          if (Thread.interrupted()) {
           System.out.println("Thread is


interrupted ");
          return;
      }

} } }
View Answers









Related Tutorials/Questions & Answers:
How can combine threads and buttons?
Explain about threads:how to start program in threads?
Advertisements
How do you combine two dictionary values for common keys
ModuleNotFoundError: No module named 'combine'
threads
ModuleNotFoundError: No module named 'tmuxp-combine'
ModuleNotFoundError: No module named 'tmuxp-combine'
ModuleNotFoundError: No module named 'blue-combine'
ModuleNotFoundError: No module named 'combine-tex'
how to create a reminder app using threads in Servlets?
threads
ModuleNotFoundError: No module named 'combine-re-compile'
cornerstone-threads - cornerstone-threads-tutorial version 2.1 Maven dependency. How to use cornerstone-threads-tutorial version 2.1 in pom.xml?
Combine String example
Combine Two Tables(Purchase and Sales) and getting Current Stock
combine two pdf files
ModuleNotFoundError: No module named 'threads'
threads in java
threads and events
combine two arrays in php
Java threads
Java - Threads in Java
ModuleNotFoundError: No module named 'mo-threads'
ModuleNotFoundError: No module named 'requests-threads'
ModuleNotFoundError: No module named 'requests-threads'
ModuleNotFoundError: No module named 'threads_creator'
ModuleNotFoundError: No module named 'Flask-Threads'
ModuleNotFoundError: No module named 'hanging-threads'
disadvantage of threads
Maven dependency for org.wildfly.core - wildfly-threads version 20.0.0.Beta3 is released. Learn to use wildfly-threads version 20.0.0.Beta3 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 20.0.0.Beta2 is released. Learn to use wildfly-threads version 20.0.0.Beta2 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 20.0.0.Beta1 is released. Learn to use wildfly-threads version 20.0.0.Beta1 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.1.Final is released. Learn to use wildfly-threads version 19.0.1.Final in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 20.0.0.Beta4 is released. Learn to use wildfly-threads version 20.0.0.Beta4 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Final is released. Learn to use wildfly-threads version 19.0.0.Final in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta18 is released. Learn to use wildfly-threads version 19.0.0.Beta18 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta17 is released. Learn to use wildfly-threads version 19.0.0.Beta17 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 18.1.2.Final is released. Learn to use wildfly-threads version 18.1.2.Final in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta15 is released. Learn to use wildfly-threads version 19.0.0.Beta15 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta14 is released. Learn to use wildfly-threads version 19.0.0.Beta14 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta13 is released. Learn to use wildfly-threads version 19.0.0.Beta13 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta12 is released. Learn to use wildfly-threads version 19.0.0.Beta12 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta11 is released. Learn to use wildfly-threads version 19.0.0.Beta11 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta10 is released. Learn to use wildfly-threads version 19.0.0.Beta10 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta9 is released. Learn to use wildfly-threads version 19.0.0.Beta9 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta8 is released. Learn to use wildfly-threads version 19.0.0.Beta8 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta7 is released. Learn to use wildfly-threads version 19.0.0.Beta7 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta6 is released. Learn to use wildfly-threads version 19.0.0.Beta6 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta5 is released. Learn to use wildfly-threads version 19.0.0.Beta5 in Maven based Java projects
Maven dependency for org.wildfly.core - wildfly-threads version 19.0.0.Beta3 is released. Learn to use wildfly-threads version 19.0.0.Beta3 in Maven based Java projects

Ads