Multi-Threading in Swing Applications
Hi guys, i'm new to this forum. me and a friend are trying to implement threads in a windows application. what we're trying to do is having an msclock updated every second so it appears on the window real-time.. this is the code we've done till now
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.util.*;
publicclass MainProgextends Frameimplements Runnable{
Button moveButt;
Label label1;
TextField t;
private Thread clockThread =null;
publicvoid start(){
if (clockThread ==null){
clockThread =new Thread(this,"Clock");
clockThread.start();
}
}
publicvoid run(){
Thread myThread = Thread.currentThread();
while (clockThread == myThread){
repaint();
try{
Thread.sleep(1000);
}
catch (InterruptedException e){
}
}
}
public MainProg(){
setLayout (new FlowLayout(FlowLayout.CENTER, 50, 50));
setTitle("Time");
label1 =new Label("Time:");
add(label1);
}
publicvoid paint(Graphics g){
// get the time and convert it to a date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
// format it and display it
DateFormat dateFormatter = DateFormat.getTimeInstance();
g.drawString(dateFormatter.format(date), 75, 120);
}
publicvoid stop (){
clockThread =null;}
}
publicstaticvoid main (String[] args)
{ Frame f =new MainProg();
f.setSize(200,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter (){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
}
with the above coding, the result is a clock displayed on screen without real-time updating. could you please suggest what we could add to make it work?
Erika
[4068 byte] By [
eka018a] at [2007-11-27 5:05:41]

> i'm trying to implement it in a window application
> not an applet.
I don't know why I thought it's an applet, sorry - but it doesn't matter. In both you'll need your content updated. I suggest reading up on the MVC pattern, by the way.
> maybe some form of loop?
You'll need at least one more Runnable that regularly triggers the update, since you can't really get a listener for changes to your data model (System time) to notify the UI to update itself.
Hi dude you should use java.util.Calendar class .
Here is an example I think this will help you .
class Test7 extends javax.swing.JFrame
{
private javax.swing.JTextField _timeField;
public Test7()
{
_timeField = new javax.swing.JTextField(5);
_timeField.setEditable(false);
_timeField.setFont(new java.awt.Font("sansserif", java.awt.Font.PLAIN, 48));
javax.swing.JPanel content = new javax.swing.JPanel();
content.setLayout(new java.awt.FlowLayout());
content.add(_timeField);
this.setContentPane(content);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener());
t.start();
}
class ClockListener implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
java.util.Calendar now = java.util.Calendar.getInstance();
int h = now.get(java.util.Calendar.HOUR_OF_DAY);
int m = now.get(java.util.Calendar.MINUTE);
int s = now.get(java.util.Calendar.SECOND);
_timeField.setText("" + h + ":" + m + ":" + s);
}
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
javax.swing.JFrame clock = new Test7();
clock.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}
});
}
}