I would like to create a periodic thread that updates my UI. The following
is a minimum example of my system:
File #1 contains the main method, and update method:
----------------------------------------------------------------------------
--
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UIClass extends JFrame{
int testVar = 0;
JLabel someLabel;
public UIClass() {
Container content = getContentPane();
someLabel = new JLabel("Init");
content.add(someLabel);
}
public void updateStatus() {
if (testVar == 0) {
System.out.println("method_test");//test to see if this if loop is
hit
someLabel.setText("testVar is 0");
}
else if (testVar == 1){
someLabel.setText("testVar is 1");
}
}
public static void main(String args[]) {
UIClass frame = new UIClass();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(200, 100);
frame.setVisible(true);
UI_updater updater = new UI_updater();//Creates a new instance of
UI_updater
updater.start();
}
}
File#2 contains the thread:
-----------------------------------------------------------------------
public class UI_updater extends Thread {
public void run() {
while (true) {
System.out.println("thread_test");
UIClass status = new UIClass();
status.updateStatus();
try {
sleep(3000);
}
catch (InterruptedException e) {}
}
}
}
But this does not update the UI. I know that the thread is created and the
if loop is hit, because the strings are printed in the console. I have tried
using "repaint()", which doess not work. Any ideas?
Regards
Martin
Harald Hein - 02 Dec 2003 20:04 GMT
> But this does not update the UI.
Use invokeLater (this should be an FAQ).
Learn about Swing's threading model (another FAQ).
Use one of the existing classes like JProgressBar or ProgressMonitor in
case your do some progress updates.
BTW: Shouldn't the FAQ be posted a little bit more often than once?
Martin Siegumfeldt - 02 Dec 2003 21:08 GMT
> > But this does not update the UI.
>
[quoted text clipped - 6 lines]
>
> BTW: Shouldn't the FAQ be posted a little bit more often than once?
Ok, thanks for the answer. It sounds as you are talking about a certain FAQ.
Do you have a link? A quick search on google yields:
http://www.ibiblio.org/javafaq/javafaq.html
but as far as I can see, it does not mention anything about invokeLater?
Regards
Martin
dave - 05 Dec 2003 12:09 GMT
Hi Martin,
Why not reference the Java site? - it has everything you require in
terms of reading.
Try this article for threads.
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
Especially look at the invokeLater() call.
Todd Corley - 03 Dec 2003 15:59 GMT
You have some REALL big misconceptions in your code.
Two biggest are:
1. You need a reference to the class you are updating.
2. Swing is not thread safe.
Anyway... here is working code showing what you were trying to do
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UIClass extends JFrame
{
int testVar = 0;
JLabel someLabel;
public UIClass()
{
Container content = getContentPane();
someLabel = new JLabel("Init");
content.add(someLabel);
}
public void updateStatus()
{
someLabel.setText("testVar is "+ ++testVar);
/*if( testVar == 0 )
{
System.out.println("method_test");//test to see if this if loop is hit
someLabel.setText("testVar is 0");
}
else if( testVar == 1 )
{
someLabel.setText("testVar is 1");
}*/
}
public static void main(String args[])
{
UIClass frame = new UIClass();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setSize(200, 100);
frame.setVisible(true);
UI_updater updater = new UI_updater( frame );//Creates a new instance of
updater.start();
}
public static class UI_updater extends Thread
{
private UIClass uiclass;
public UI_updater( UIClass uiclass )
{
this.uiclass = uiclass;
}
public void run()
{
while( true )
{
System.out.println("thread_test");
try
{
SwingUtilities.invokeAndWait( new Runnable()
{
public void run()
{
uiclass.updateStatus();
}
});
}
catch( Exception exc )
{
exc.printStackTrace();
}
try
{
sleep(3000);
}
catch( InterruptedException e )
{
}
}
}
}
}
Martin Siegumfeldt - 06 Dec 2003 12:12 GMT
> You have some REALL big misconceptions in your code.
> Two biggest are:
[quoted text clipped - 73 lines]
> public void run()
> {
uiclass.updateStatus();
> }
> });
[quoted text clipped - 15 lines]
> }
> }
Thank you gentlemen for your time. I made it work by use of this last
proposal. .
I really appreciate it
Regards
Martin