• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use infinite loops?  RSS feed

 
vojin djukic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm writing a graphical program with several Label objects. One of them is supposed to constantly change color. I tried to do it with while loop like this:

while (true) {label1.setColor(Color.blue); pause (80); label1.setColor(Color.red); pause (80);}

However, the rest of the code (after the loop) is never reached because the loop never ends. Is it possible to use infinite loop like this? And, is there any other way to handle permanent processes that are supposed to run as long as the program is running (like, in this case, blinking Label)?
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vojin djukic wrote:Is it possible to use infinite loop like this? And, is there any other way to handle permanent processes that are supposed to run as long as the program is running (like, in this case, blinking Label)?


Yes, and Yes.

You will have to use two different threads. One thread will run the loop and another thread will run the main program.

And if you set the loop handling thread as daemon thread, it will be stopped when your main program is terminated.

I am a beginner, but I believe there are better ways to handle this scenario using the GUI library instead of creating a new thread.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:(...)
I am a beginner, but I believe there are better ways to handle this scenario using the GUI library instead of creating a new thread.

For a java Swing GUI program, there is indeed a simple solution: the Swing Timer class.
You do the updating of the color in (or via) the 'actionPerformed' method.

See the api: http://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html

Greetz,
Piet
 
vojin djukic
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I managed to do it with creating a new Thread. A simple way how to do it: http://www.javamex.com/tutorials/threads/
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vojin djukic wrote:Thanks. I managed to do it with creating a new Thread. A simple way how to do it: http://www.javamex.com/tutorials/threads/


Yeah, be careful with this. Swing isn't thread safe -- you will need to adjust for Swing. Or you can see Piet's previous post.

Henry
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That tutorial is quite good, and warns against creating a new thread. Using invokeLater() only creates a Thread if the event dispatch thread (EDT) isn't already in existence. If it is already running, invokeLater simply plugs into the EDT. Unfortunately they do not show how to use SwingWorker or similar to run a thread in parallel with your Swing application. Start reading here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!