• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads... changing while loop....

 
Javeo Lineo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Can you help because my I need to change the something regarding Threads, my friend told me not to use "while loop" but instead try to use a "Timer", but I'm not familiar with it... aside from timer what else can I use? If ever I will use a "Timer" how do I convert "While loop" to a "Timer"?

Here is the code:



// Thread
private Thread ObjectThread = null;

// Holds the current progress
public static int ProgressInt = 0;

// Constant for maximum progress
private static final int MAX = 100;

// Progress bar
private JProgressBar ProgressBar;

// Method for starting up the Thread.
public void start()
{
if (ObjectThread == null)
{
ObjectThread = new Thread(this, "");
ObjectThread.start();
}
}


// Method for update of progress bar
public void run()
{
while (ProgressInt <= MAX)
{
ProgressBar.setValue(ProgressInt);
if (ProgressInt >= MAX)
{
break;
}
}
setVisible(false);
}
 
Olaf Kummer
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Javeo Lineo:
Can you help because my I need to change the something regarding Threads, my friend told me not to use "while loop" but instead try to use a "Timer", but I'm not familiar with it...


Your friend refers to javax.swing.Timer. See the javadoc of that class, it is helpful.

Other than that, a general rule is to modify Swing objects in the AWT event queue thread and only there. Modifying them in other thread can cause bizzare and typically undesired effects.

Note that you might (alternatively) encapsulate the setting of the global variable in a method, which updates the Swing component. The update would have to be decoupled from the setter by means of the method javax.swing.SwingUtilities.invokeLater(Runnable). Here, too, I'd like to point at the javadoc.

Regards, Olaf
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!