What is the best strategy to create a screen part of a java swing application, that repaints itself every minute with new information from a web page? (A yahoo stock quote for example) Thank you.
5 Answers
1. Create a Separate thread apart from the GUI thread(ie Event Dispatcher Thread).
2. Make it call the service that give the yahoo stock with delay of 60 sec, use Thread.sleep(60000);
3. call repaint();
Edited:
new Thread(new Runnable(){
public void run(){
try{
while (true){
Thread.sleep(60000);
yahoo() // CALL TO YAHOO SENSEX
repaint();
}
}catch(Exception ex){
}
}).start();

- 33,294
- 6
- 48
- 75
-
Can you give a small example on this?Thnx. – skiabox Jul 07 '12 at 19:23
-
java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainForm().setVisible(true); } }); – skiabox Jul 07 '12 at 19:39
-
You can put the code i wrote into a method..and then call it just once from main() after EventQueue.invokeLater, then it will keep looping.. i have not added here any mechanism to stop it.. you can do that using boolean.. – Kumar Vivek Mitra Jul 07 '12 at 19:42
-
1please use `invokeLater` inside the thread to update GUI components as exemplified [here](http://stackoverflow.com/a/11378086/1001027). This is safer to avoid concurrence problems. – Francisco Spaeth Jul 07 '12 at 19:44
-
@kumar-vivek-mitra I have problems setting a value to a JLabel because all actions are coming from static void main and the compiler complains because JLabel variables are not static.Any ideas how to overcome this? – skiabox Jul 08 '12 at 11:23
-
Netbeans does not allow me to add the static keyword to the specific JLabel declaration! – skiabox Jul 08 '12 at 11:34
-
Fixed that annoyance...all you have to do is right click at the component and then click at Customize Code – skiabox Jul 08 '12 at 11:38
-
repaint() is not working because it is being called from a static context. – skiabox Jul 08 '12 at 11:54
-
You need to make the object of the class which is the container in the main, and then call the functions.. – Kumar Vivek Mitra Jul 08 '12 at 13:41
-
It seem to work as it is, I'll watch it tomorrow when the markets are open. – skiabox Jul 08 '12 at 15:18
-
I didnt get you, "It seem to work as it is" ... what does that mean... Its working or not.. – Kumar Vivek Mitra Jul 08 '12 at 18:06
Use Timer
with TimerTask
classes to do some action on regular basis. And send "update" event to view-component (in MVC-notation) of your application.
You could start a new Thread
and in this thread execution you could put events on the event stack as exposed on the sample below. It is more safe to implement this way because the GUI update will be invoked from the event dispatch thread. More information here.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class SpringcConc {
final static JLabel label = new JLabel("INITIALIZED ...");
public static void main(final String[] s) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame myJframe = new JFrame();
myJframe.add(label);
myJframe.pack();
myJframe.setVisible(true);
instituteThreads();
}
});
}
protected static void instituteThreads() {
Thread queryThread1 = new Thread() {
@Override
public void run() {
while (true) {
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
label.setText("THREAD 1 UPD");
}
});
}
}
};
queryThread1.start();
Thread queryThread2 = new Thread() {
@Override
public void run() {
while (true) {
try {
sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
label.setText("THREAD 2 UPD");
}
});
}
}
};
queryThread2.start();
}
}
Another approach would be using Timer:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SwingTimer {
public static void main(final String[] args) {
final JLabel label = new JLabel("INITIALIZED");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame jFrame = new JFrame();
jFrame.add(label);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setBounds(200, 200, 100, 50);
}
});
new Timer(500, new ActionListener() {
public void actionPerformed(final ActionEvent e) {
label.setText("UPDATE 1");
}
}).start();
new Timer(750, new ActionListener() {
public void actionPerformed(final ActionEvent e) {
label.setText("UPDATE 2");
}
}).start();
}
}

- 23,493
- 7
- 67
- 106
-
I just updated the sample, you can put into the `run` method. – Francisco Spaeth Jul 07 '12 at 19:46
Instead of a separate thread or java.util.Timer
, use javax.swing.Timer
to pace the updates to your data model, as shown here. The advantage is that a "Swing timer's task is performed in the event dispatch thread."
Probably the best solution in use a new thread, that download information & repaints itself..

- 1,781
- 3
- 16
- 34