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
how to create a reminder app using threads in Servlets?
threads
ModuleNotFoundError: No module named 'blue-combine'
ModuleNotFoundError: No module named 'combine-tex'
ModuleNotFoundError: No module named 'tmuxp-combine'
ModuleNotFoundError: No module named 'tmuxp-combine'
ModuleNotFoundError: No module named 'combine-re-compile'
Combine String example
combine two pdf files
ModuleNotFoundError: No module named 'threads'
Combine Two Tables(Purchase and Sales) and getting Current Stock
threads in java
threads and events
combine two arrays in php
Java threads
Java - Threads in Java
disadvantage of threads
ModuleNotFoundError: No module named 'Flask-Threads'
ModuleNotFoundError: No module named 'hanging-threads'
ModuleNotFoundError: No module named 'mo-threads'
ModuleNotFoundError: No module named 'requests-threads'
ModuleNotFoundError: No module named 'requests-threads'
ModuleNotFoundError: No module named 'threads_creator'
Sync Threads
regardoing multi threads - Java Beginners
ModuleNotFoundError: No module named 'signalr-client-threads'
Running threads in servlet only once - JSP-Servlet
java threads - Java Beginners
threads in java
Threads,Servlets - Java Beginners
threads
Threads
Threads
Threads
Threads
Synchronized Threads
Version of cornerstone-threads>cornerstone-threads-api dependency
Version of cornerstone-threads>cornerstone-threads-tutorial dependency
Version of cornerstone-threads>cornerstone-threads-impl dependency
Threads in realtime projects
threads & autorelease pool
Daemon Threads
Synchronized Threads
Synchronized Threads
Coding for life cycle in threads

Ads