Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Stop a while loop with an awt button, problem

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stop a while loop with an awt button, problem

    This is my simplified code: (how can I get this work?)
    public class MyClass extends Applet implements ActionListener{
    	public void init(){
    		Button start = new Button("start");
    		Button stop = new Button("stop");
    		add(start);
    		add(stop);
    		start.addActionListener(this);
    		stop.addActionListener(this);
    		start.setActionCommand("start");
    		stop.setActionCommand("stop");
    	}
     
    	public void actionPerformed(ActionEvent ae){
    		String action = ae.getActionCommand();
     
    		if(action.equals("start")){
    			// start inputstream
    			while((len = is.read(buffer)) != -1){
    				// inputstream stuff
    			}
    		} else if(action.equals("stop")){
    			// stop while
    		}
    	}
    }

    thanks!
    Last edited by frx08; January 19th, 2011 at 07:35 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Stop a while loop with an awt button, problem

    I'm not convinced this is the best way to achieve your goal.

    But to answer your question, you have to do the loop outside of your ActionListener (and not on the EDT). In that loop, check against a variable and exit the loop when it changes. Change that variable when you press the button.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    JavaPF (January 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stop a while loop with an awt button, problem

    I created a new class to manage the input stream
    but the button is still stuck.. I can't click it

    public StreamClass{
        public void doTheJob(){
            // start inputstream
            while((len = is.read(buffer)) != -1){
                // inputstream stuff
            }
        }
    }
    ...
    public class MyClass extends Applet implements ActionListener{
        public void init(){
            Button start = new Button("start");
            Button stop = new Button("stop");
            add(start);
            add(stop);
            start.addActionListener(this);
            stop.addActionListener(this);
            start.setActionCommand("start");
            stop.setActionCommand("stop");
        }
     
        public void actionPerformed(ActionEvent ae){
            String action = ae.getActionCommand();
     
            if(action.equals("start")){
               StreamClass in = new StreamClass();
               in.doTheJob();
            } else if(action.equals("stop")){
                // stop while
            }
        }
    }

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Stop a while loop with an awt button, problem

    If the task is time consuming, place it in a Thread of its own or use SwingWorker. As is, the time consuming task is on the EDT. Given the EDT is responsible for the GUI, no updates will be made until the task is complete - making it seem like your program froze. If you wish to stop the process in mid-stride, take KevinWorkman's advice re checking against a variable

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Location
    Lolo, Mt
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stop a while loop with an awt button, problem

    try using a boolean variable telling it to run while end = false and after your requirements set end to true

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Stop a while loop with an awt button, problem

    Quote Originally Posted by SHStudent21 View Post
    try using a boolean variable telling it to run while end = false and after your requirements set end to true
    That's pretty much exactly what I suggested in the first reply.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 01:23 PM
  2. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 12:23 PM
  3. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 05:40 AM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 05:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 03:31 PM

Tags for this Thread