Solved: JAVA: Error in Loops?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

sepala

Thread Starter
Joined
May 20, 2010
Messages
4,038
Hi all,

I am trying to create a small java software which counts from 0-10 then 10-0, and it will display the result but placing the values in the JLabel. This is what I have done.

Code:
// Java Document
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class counter extends JFrame implements ActionListener
{
    JLabel label1;

    JButton button1, button2;

    String answer="The Number is: ";

    public counter()
    {
        label1 = new JLabel("");

        button1 = new JButton("OK");
                button1.addActionListener(this);
        button2= new JButton("CANCEL");
                button2.addActionListener(this);

        JPanel panel1 = (JPanel)getContentPane();
        panel1.setLayout(new FlowLayout());

                panel1.add(label1);
                panel1.add(button1);
                panel1.add(button2);

    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource().equals(button1))
                {

                    while(true)
                    {
                        for(int a=0; a<10; a++)
                        {
                            label1.setText(answer+a);
                        }

                        for(int c=10; c>0; c--)
                        {
                            label1.setText(answer+c);
                        }
        
                    }
                }
        else
        {
            System.exit(0);
        }
    }

    public static void main(String[]args)
    {
        counter c = new counter();
        c.setVisible(true);
        c.setSize(500,500);
        c.validate();
    }
}
This is not working. I tried to do that buy removing the While loop, then it displayed only one number, not the particular numbers as it counts. Please help.
 
Joined
Jul 2, 2006
Messages
379
Well, the while(true) puts the program in a infinite loop as there is never anything to break out of the loop so you need to either remove it, or create a condition where the loop breaks (e.g. a key press)

When you remove the while loop, the problem with having the settext method in a loop is that the loop is running on the same thread as the screen repaints so the loop will execute, then the screen will repaint having missed all the action!

If you want to fix this, you'll need to create a new thread to run the loop on :

Code:
public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource().equals(button1))
        {
        	new Thread(new Runnable() {
				
				public void run() {
					for(int a=0; a<10; a++)
                    {
                        label1.setText(answer+a);
                        try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
                    }

                    for(int c=10; c>0; c--)
                    {
                        label1.setText(answer+c);
                        try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
                    }
				}
			}).start();
                    
        	
        }
        else
        {
            System.exit(0);
        }
    }
 

sepala

Thread Starter
Joined
May 20, 2010
Messages
4,038
WOW!!!! I haven't learn Threads yet andy! It is in next Tuesday class!! This is gonna help me a lot!!!! Anyway, I tried the something same(It changes the backgroumd color) in C# using "Visible" as the condition to the while loop. In java, i think visible is not a command..

Thanks andy...Thanks alot...
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top