Results 1 to 8 of 8
- 02-22-2011, 09:13 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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 10:49 AM.
- 02-22-2011, 09:38 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
That else if has no idea about print100 as it is local to the if statement above it.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(); } }
Also you are not testing the thread for an interruption in void run()
Have a read here:
Interrupts (The Java™ Tutorials > Essential Classes > Concurrency)
- 02-23-2011, 06:06 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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 10:50 AM.
- 02-23-2011, 06:17 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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 10:50 AM.
- 02-23-2011, 06:27 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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; } } } }
- 02-24-2011, 07:55 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
I am sorry. The script can start and stop, but I get an error when I try to restart it again.
Any suggestions?
- 02-24-2011, 08:50 PM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Yeah, post the EXACT error
- 02-24-2011, 09:06 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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
-
can i combine this 2 code into one?
By reeveliew in forum New To JavaReplies: 3Last Post: 05-09-2010, 02:24 PM -
How can I do this? Combine variable.
By PeterFeng in forum New To JavaReplies: 5Last Post: 01-14-2009, 05:44 PM -
[SOLVED] How do I combine two variables?
By bobleny in forum New To JavaReplies: 12Last Post: 07-08-2008, 03:15 PM -
How to combine mysql and java?
By sandeeprao.techno in forum Advanced JavaReplies: 1Last Post: 05-21-2008, 04:41 AM -
Combine package of(jdk,eclipse and tomcat)
By joseph in forum EclipseReplies: 0Last Post: 04-07-2008, 01:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks