Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread.sleep() problem 1

Status
Not open for further replies.

jared5494

Technical User
Jun 9, 2002
111
US
Hello,
I am trying to just make a simple applet to count slowly from 1-5, pausing in between. However, when i go to run the application, it just pauses to display the application and only shows the last number, not the counting:

Code:
import java.awt.*;
import javax.swing.*;

public class SightWords extends JFrame
{
	private JPanel mainPanel;
	private JTextField mainField;

	public SightWords()
	{
		mainPanel = new JPanel();
		mainField = new JTextField("",15);

		for (int i=0; i<5; i++)
		{
			try
			{
				Thread.sleep (1000);
			}
			catch ( Exception ex )
			{
				ex.printStackTrace();
			}

			mainField.setText(String.valueOf(i));

		}

		mainPanel.add(mainField);
		add(mainPanel);
	}
}

any help would be greatly appreciated. (note* i am quite a newb when it comes to java, so explainations would be greatly appreciated!)

thanks in advance,
Jared
 
I think you need to use a thread for the counting. Your applet has to implement Runnable interface, and you need to implement the run() method to do the couting.
I think your applet also needs to extend JApplet class

Code:
public class SightWords extends JApplet{
   private JPanel mainPanel;
   private JTextField mainField;

   public void init(){}

   public void run(){}
 }





 
I don't think so.

Try repainting the applet after each count.

Cheers,
Dian
 
I believe you are correct Diancecht, I'm just posting the code because I'm very oh so very rusty in Java and I'm wanting to familiarize myself with it.

Code:
public class SightWords extends JFrame
{
    private JPanel mainPanel;
    private JTextField mainField;

    public SightWords()
    {
        mainPanel = new JPanel();
        mainField = new JTextField("",15);

        for (int i=0; i<5; i++)
        {
            try
            {
                Thread.sleep (1000);
            }
            catch ( Exception ex )
            {
                ex.printStackTrace();
            }

            mainField.setText(String.valueOf(i));
            mainPanel.add(mainField);
            add(mainPanel);
        }
    }
}

[monkey][snake] <.
 
repainting seems not neccessary, and multiple add (mainpanel) is the very wrong way to go.

Of course, if you first count and pause, and add the textfield later, counting is done.

First add the Field, then change the value:
Code:
 import java.awt.*;
import javax.swing.*;

public class SightWords extends JFrame
{
	private JPanel mainPanel;
	private JTextField mainField;

	public SightWords ()
	{
		mainPanel = new JPanel ();
		mainField = new JTextField ("", 15);
		mainPanel.add (mainField);
		add (mainPanel);
		setSize (260, 200);
		setVisible (true);
		for (int i=0; i<50; i++)
		{
			try
			{
				Thread.sleep (100);
			}
			catch (Exception ex)
			{
				ex.printStackTrace ();
			}
			mainField.setText (String.valueOf (i));
		}
	}

	public static void main (String args [])
	{
		new SightWords ();
	}
}

don't visit my homepage:
 
stefanwagner,
you were right on the money and thank you so much!

-Jared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top