Sleepers

can someone explain to me what sleepers are and what they are used for?

i'm trying to use one in my program and i don't think it works.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass Title

{

JFrame f1;

JPanel p1;

JLabel Story;

Container c1;

boolean end;

public Title()

{

f1 =new JFrame("HOBO");

f1.setSize(750,650);

c1 = f1.getContentPane();

p1 =new JPanel();

end =false;

Thread runner =new Thread();

while (end=false)

{

try

{

runner.sleep(2);

}

catch (InterruptedException e){}

for (int i=0; i<5; i++)

{

if (i==1)

Story =new JLabel("A Long time ago....");

if (i==2)

Story =new JLabel("there was a hobo...");

if (i==3)

Story =new JLabel("who was old...");

if (i==4)

Story =new JLabel("and homeless...");

if (i==5)

Story =new JLabel("and kicked people's butts for money");

p1.add(Story);

}

end =true;

}

c1.add(p1);

f1.show();

}

}

did i put it in the wrong place?

[2596 byte] By [ichbinsterbena] at [2007-11-27 3:14:34]
# 1

Thread.sleep is a static method. when you invoke it, you put the thread that it's called from to sleep, not the thread you try and invoke it on. always call it like this

Thread.sleep(1000);

rather than

Thread mythread = new Thread();

mythread.sleep(); // will cause THIS thread to sleep, not mythread

a common mistake

but there's nothing here called a "sleeper" - a unique mistake

georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 2
other than that am i using it right?because i want the text to show up one at a time.
ichbinsterbena at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 3
no. you can't put a thread to sleep from within another thread, the thread has to put itself to sleep, using Thread.sleep()
georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 4
so do i take it out of where i have it?
ichbinsterbena at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 5
> so do i take it out of where i have it?you can't work that out yourself?
georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 6
well read the code, i need the text to show up individually, and i was told to us sleep
ichbinsterbena at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 7
> well read the code, i need the text to show up> individually, and i was told to us sleepyou have everything you need right here in this thread, if you stop and think for a minute. I like to help people, but I don't feel like I'm helping if I just spoon-feed them
georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 8
so ur saying i don't need the thread?or do i put it in the for() statement?honestly i don't know what threads are.Message was edited by: ichbinsterben
ichbinsterbena at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 9
there is no for in the code. actually, looking at the code, I can't really see why you've got that thread there, it doesn't do anything. it's unclear what you're trying to do, to be honest
georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 10

its there.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Title

{

JFrame f1;

JPanel p1;

JLabel Story;

Container c1;

boolean end;

public Title()

{

f1 = new JFrame("HOBO");

f1.setSize(750,650);

c1 = f1.getContentPane();

p1 = new JPanel();

end = false;

while (end=false)

{

try

{

Thread.sleep(1);

}

catch (InterruptedException e) {}

for (int i=0; i<5; i++)

{

if (i==1)

Story = new JLabel("A Long time ago....");

if (i==2)

Story = new JLabel("there was a hobo...");

if (i==3)

Story = new JLabel("who was old...");

if (i==4)

Story = new JLabel("and homeless...");

if (i==5)

Story = new JLabel("and kicked people's butts for money");

p1.add(Story);

}

end = true;

}

c1.add(p1);

f1.show();

}

}

i need those JLabels (i'll change them to individuals eventually) show up one at a time

Message was edited by:

ichbinsterben

Message was edited by:

ichbinsterben

ichbinsterbena at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 11
> honestly i don't know what threads are. http://java.sun.com/docs/books/tutorial/essential/concurrency/
hunter9000a at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 12
> its there.it's not
georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 13
for(int i=0;i<5;i++)....Message was edited by: ichbinsterben
ichbinsterbena at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 14
> for(int i=0;i<5;i++)....> > Message was edited by: > ichbinsterbenlol I do apologise. I saw the while loop and that was it!before you go stampeding off using threads, you need to understand them a bit. follow hunter's link
georgemca at 2007-7-12 8:17:05 > top of Java-index,Java Essentials,New To Java...
# 15
hunter's link takes me to a "Lesson" on how a computer processes. unless that's what threads are
ichbinsterbena at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...
# 16
> hunter's link takes me to a "Lesson" on how a> computer processes. unless that's what threads areThere's more to it than just the intro paragraph. See the links on the left? See the Next link at the bottom?
hunter9000a at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...
# 17
my bad
ichbinsterbena at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...
# 18
This program won't work anyway because the statement "end=false" will allways return false and therefore the while loop will never run. You wanted "end == false" as the first statement assigns instead of comparing.
madformusea at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...
# 19

> This program won't work anyway because the statement

> "end=false" will allways return false and therefore

> the while loop will never run. You wanted "end ==

> false" as the first statement assigns instead of

> comparing.

good spot. or even better, just while(!end)

georgemca at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...
# 20

Also this will never work as you want it to since you only show the GUI at the end and all of the operations on the JLabel are done before the GUI is shown.

If you want the same JLabel to change, you should do story.setText(), if not, you should have different JLabel's.

The Thread.sleep(1000) should be inside the for loop if you want to pause between each execution of for loop statement, ie:

for (int i=0; i<5; i++)

{

Thread.sleep(1000);

if (i==1)

Story = new JLabel("A Long time ago....");

if (i==2)

Story = new JLabel("there was a hobo...");

if (i==3)

Story = new JLabel("who was old...");

if (i==4)

Story = new JLabel("and homeless...");

if (i==5)

Story = new JLabel("and kicked people's butts for money");

}

So if you replace Story = new JLabel with Story.setText and put the for loop after the GUI is shown you will have something closer to what your looking for I believe.

phsaboa at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...
# 21
phsabo thanx it helped a lot.
ichbinsterbena at 2007-7-21 20:42:40 > top of Java-index,Java Essentials,New To Java...