• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

threads and display

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am having a hard time coming up with the best way to display output in a thread. Right now my program works but the output (a clock) is displayed to the comand window. I would like to have it displayed to a new window. When I tried this, instead of the thread updating the time it created a new window, so I had windows poping up all over. I also had trouble figuring out how to pass a booleen (when the user presses a button on the window that displays the time) to stop the while loop. Here is what I have so far that works.

What I would like to know is what is the best way to percede:
1) a seperate class for the display
2) use one class and implement runnable instead of extending thread
should I take the creation of the clock string out of the while loop?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I tried this, instead of the thread updating the time it created a new window, so I had windows poping up all over.
Sounds like you were creating a new window inside the while loop. Try creating your window before the while loop (while still inside the run() method) so that it happens just once per thread. Then update the window from within the loop.
I also had trouble figuring out how to pass a booleen (when the user presses a button on the window that displays the time) to stop the while loop.
This is a standard problem when dealing with threads - how to make it cancelable? Basically, create cancel() method which changes some instance variable to indicate the thread should stop, and have your run() method periodically check the state of that variable. See examples in Sun's Java tutorial or here (in the "What should I use instead of Thread.stop?" section).
[ July 29, 2002: Message edited by: Jim Yingst ]
 
jo tay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim!
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jo,
I think i have a code for u.
You can a simple textarea in a scroll pane and two buttons Start, Stop & write the following code in their rescpective actionPerformed :-

void jButtonStart_actionPerformed(ActionEvent e)
{
ThreadDemo td = new ThreadDemo() ;
ThreadDemo.flag = true ;
/*System.out.println("In start method");
System.out.println(td);*/
td.start();
}
void jButtonstop_actionPerformed(ActionEvent e)
{
ThreadDemo.flag = false ;
}

And the ThreadDemo class is here :

public class ThreadDemo extends Thread
{
static boolean flag = true ;
//To start counting from where the thread last terminated.
private static int i ;
public void run()
{
while(true)
{
if(!flag)
break ;
if(i==1000)
i = 0 ;
jFrameMenu.jTextArea1.append(new Integer(i).toString()
+"/");
i++ ;
}
jFrameMenu.jTextArea1.append("\nThe thread terminated\n");
}
}

[ August 21, 2002: Message edited by: Anirban dutta ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic