Results 1 to 8 of 8
  1. #1
    jnms is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default How to combine Buttons and threads

    I want to make an app with two buttons. With the start button I want to start something (System.out.println(" " + i); print a lot of numbers ) and with the stop button I want to be able to stop it
    I made the following script. But the print100.interrupt(); does not stop the thread.

    What am I doing wrong?


    Java Code:
    package testactionevent;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @author
     */
    public class TestActionEvent extends JFrame implements ActionListener{
        private JButton jbtStart = new JButton("Start");
        private JButton jbtStop = new JButton("Stop");
    
    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){
                Thread print100 = new Thread(new SiThread(10000));
                print100.start();
                print100.interrupt();
            }
            else if (e.getSource() == jbtStop){
                print100.interrupt();
            }
        }
    }
        
    class SiThread implements Runnable {
        private int lastNum;
        public SiThread(int n){
            lastNum = n;
        }
    
        public void run() {
            for (int i = 0; i <= lastNum; i++) {
                System.out.println(" " + i);
        }
    }
    }
    Last edited by jnms; 02-23-2011 at 11:49 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    public void actionPerformed(ActionEvent e){
          if (e.getSource() == jbtStart){
              Thread print100 = new Thread(new SiThread(10000));
              print100.start();
              print100.interrupt();
          }
          else if (e.getSource() == jbtStop){
             print100.interrupt();
           }
    }
    That else if has no idea about print100 as it is local to the if statement above it.
    Also you are not testing the thread for an interruption in void run()

    Have a read here:
    Interrupts (The Java™ Tutorials > Essential Classes > Concurrency)

  3. #3
    jnms is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    After reading your link for concurrency, I changed the class like this.
    But I don't understand the local print100. Can I change that to a non local? If so how and where?
    Java Code:
    class SiThread implements Runnable {
        private int lastNum;
        public SiThread(int n){
            lastNum = n;
        }
    
        public void run() {
            for (int i = 0; i <= lastNum; i++) {
                System.out.println(" " + i);
                  if (Thread.interrupted()) {
            return;
        }
        }
    }
    }
    Last edited by jnms; 02-23-2011 at 11:50 AM.

  4. #4
    jnms is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    I changed it to. But this does not stop the thread.
    Please advise.:confused:

    Java Code:
     public void actionPerformed(ActionEvent e){
            Thread print100 = new Thread(new SiThread(10000));
            if (e.getSource() == jbtStart){
               
                print100.start();
            }
            else if (e.getSource() == jbtStop){
    
                print100.interrupt();
            }
        }
    Last edited by jnms; 02-23-2011 at 11:50 AM.

  5. #5
    jnms is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    I think this does the trick. I moved the "Thread print100 = new Thread(new SiThread());"

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package testactionevent;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @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){
               
                print100.start();
            }
            else if (e.getSource() == jbtStop){
    
                print100.interrupt();
            }
        }
    }
        
    class SiThread implements Runnable {
        public void run() {
            boolean lopend = false;
            int num =0;
            System.out.println(" " + num);
            while (lopend == false){
                num = num +1;
                System.out.println(" " + num);
                if (Thread.interrupted()) {
                    return;
                }
            }
        }
    }

  6. #6
    jnms is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Angry

    I am sorry. The script can start and stop, but I get an error when I try to restart it again.
    Any suggestions?

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Yeah, post the EXACT error

  8. #8
    jnms is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Think it's solved.
    :)
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package testactionevent;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @author
     */
    public class TestActionEvent extends JFrame implements ActionListener{
        private JButton jbtStart = new JButton("Start");
        private JButton jbtStop = new JButton("Stop");
        SiThread draadje;
    
    public TestActionEvent(){
        draadje = new SiThread();
        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){
    
                Thread thread = new Thread(draadje);
                thread.start();
            }
            else if (e.getSource() == jbtStop){
                draadje.stop();
            }
        }
    }
    
    class SiThread implements Runnable {
        int i = 0;
        boolean lopend = false;
    
        public void run() {
            lopend = true;
            while (lopend){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex){
                    ex.printStackTrace();
                }
               i++;
               System.out.println(i);
            }
        }
    
        public void stop(){
            lopend = false;
        }
    }

Similar Threads

  1. can i combine this 2 code into one?
    By reeveliew in forum New To Java
    Replies: 3
    Last Post: 05-09-2010, 03:24 PM
  2. How can I do this? Combine variable.
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 01-14-2009, 06:44 PM
  3. [SOLVED] How do I combine two variables?
    By bobleny in forum New To Java
    Replies: 12
    Last Post: 07-08-2008, 04:15 PM
  4. How to combine mysql and java?
    By sandeeprao.techno in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 05:41 AM
  5. Combine package of(jdk,eclipse and tomcat)
    By joseph in forum Eclipse
    Replies: 0
    Last Post: 04-07-2008, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •