1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: JAVA: Error in Loops?

Discussion in 'Software Development' started by sepala, Jan 20, 2011.

Thread Status:
Not open for further replies.
  1. sepala

    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.
     
  2. andythepandy

    andythepandy

    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);
            }
        }
    
     
  3. sepala

    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...
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/975808

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice