Strange SWING Behaviour
Hi.
Please copy/Paste the following code
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
publicclass timerextends JFrameimplements Runnable{
Frame frame;
Thread timer;
private JLabel labelSec, labelMin, labelHour, labelSep1,labelSep2;
private JButton button;
privateint secCount =0, minCount =0, hourCount=0;
SimpleDateFormat formatter ;
Date currentDate;
timer(){
super("Timer!");
Container c = getContentPane();
c.setLayout(new FlowLayout() );
//JFrame frame = new JFrame();
//Panel myPanel = new Panel();
labelMin =new JLabel("00");
labelHour =new JLabel("00");
labelSep1 =new JLabel(":");
labelSep2 =new JLabel(":");
labelSec =new JLabel("00");
button =new JButton("Stop");
c.add(labelHour);
c.add(labelSep1);
c.add(labelMin);
c.add(labelSep2);
c.add (labelSec);
c.add(button);
//frame.add( myPanel);
runClock();
button.addActionListener(new ActionListener(){
publicvoid actionPerformed( ActionEvent e){
timer.interrupt();
}
}
);
//frame.setBackground(Color.blue);
//frame.set
Visible(true);
setSize(200,100);
setResizable(false);
show();
}
publicvoid paint(Graphics g){
secCount += 1;
labelSec.setText(String.valueOf(secCount));
if ( Integer.parseInt(labelSec.getText()) >= 60 ){
minCount += 1;
labelMin.setText(String.valueOf(minCount));
if( Integer.parseInt(labelMin.getText()) >= 60 ){
hourCount += 1;
labelHour.setText(String.valueOf(hourCount));
minCount = 0;
labelMin.setText(String.valueOf(0));
}
labelSec.setText(String.valueOf(0));
secCount = 0;
}
}
privatevoid runClock(){
timer =new Thread(this);
timer.start();
}
publicvoid run(){
Thread me = Thread.currentThread();
while (timer == me){
try{
Thread.currentThread().sleep(1000);
}catch (InterruptedException e){
}
repaint();
}
}
publicstaticvoid main (String args[]){
timer app =new timer();
app.addWindowListener(new WindowAdapter(){
publicvoid windowClosing( WindowEvent e){
System.exit(0);
}
}
);
}
}
When the window is moved , the timer "speeds up" !!!!
Does anyone have a solution to this problem?
Thankx in advance...
[5433 byte] By [
alikamran] at [2007-9-26 7:32:07]

> Hi.
> Please copy/Paste the following code
>
> >
> import javax.swing.*;
> import javax.swing.event.*;
> import java.awt.event.*;
> import java.text.*;
> import java.util.*;
>
> public class timer extends JFrame implements
> Runnable{
> Frame frame;
> Thread timer;
> private JLabel labelSec, labelMin, labelHour,
> labelSep1,labelSep2;
> private JButton button;
> private int secCount =0, minCount =0, hourCount=0;
> SimpleDateFormat formatter ;
> Date currentDate;
>
> timer() {
> super("Timer!");
> Container c = getContentPane();
> c.setLayout( new FlowLayout() );
> //JFrame frame = new JFrame();
> //Panel myPanel = new Panel();
>
> labelMin = new JLabel("00");
> labelHour = new JLabel("00");
> labelSep1 = new JLabel(":");
> labelSep2 = new JLabel(":");
> labelSec =new JLabel("00");
>
> button = new JButton("Stop");
> c.add(labelHour);
> c.add(labelSep1);
> c.add(labelMin);
> c.add(labelSep2);
> c.add (labelSec);
> c.add(button);
> //frame.add( myPanel);
>
> runClock();
>
> button.addActionListener( new ActionListener() {
> public void actionPerformed( ActionEvent e) {
> timer.interrupt();
> }
> }
> );
> //frame.setBackground(Color.blue);
>
> //frame.set
> Visible(true);
> setSize(200,100);
> setResizable(false);
> show();
>
> }
>
> public void paint(Graphics g) {
>
> secCount += 1;
> labelSec.setText(String.valueOf(secCount));
>
> if ( Integer.parseInt(labelSec.getText()) >= 60 ) {
> minCount += 1;
> labelMin.setText(String.valueOf(minCount));
> if( Integer.parseInt(labelMin.getText()) >= 60 ) {
> hourCount += 1;
> labelHour.setText(String.valueOf(hourCount));
> minCount = 0;
> labelMin.setText(String.valueOf(0));
>
> }
>
> labelSec.setText(String.valueOf(0));
> secCount = 0;
> }
> }
>
> private void runClock() {
> timer = new Thread(this);
> timer.start();
> }
>
> public void run() {
> Thread me = Thread.currentThread();
> while (timer == me) {
> try {
> Thread.currentThread().sleep(1000);
> } catch (InterruptedException e) {
> }
> repaint();
> }
> }
>
> public static void main (String args[]) {
> timer app = new timer();
>
> app.addWindowListener( new WindowAdapter() {
> public void windowClosing( WindowEvent e) {
> System.exit(0);
> }
> }
> );
> }
> }
>
>
>
> When the window is moved , the timer "speeds up" !!!!
>
> Does anyone have a solution to this problem?
>
> Thankx in advance...
The behaviour is not strange, specially when you increment a counter in a method which is the trigger action for paint events.