JLabels and Timers

Hi, I'm working on a audio playing program that uses JLabels to display the time left until the song is finished playing and another label that counts up displaying how far into the song it is. When I read the Swing tutorial on Timers, it said not to use the java.util Timer, and so I purposely tried to avoid using that. However, as the program counts up and down, every now and then (this is common), the JLabels will freeze and then after a minute or so instantly change and skip to where the time displayed should be and then resume counting until another freeze later on. I know it's not my computer because a bunch of other threads in my program run nice and smoothly during this process. This is my Timer code:

timer =new Timer

(

1000,//Call this method every second

new ActionListener()

{

publicvoid actionPerformed(ActionEvent e)

{

//First make sure the time is incremented correctly

timeSecs++;

if (timeSecs == 60)

{

timeSecs = 0;

timeMins++;

if (timeMins == 60)

{

timeHrs++;

timeMins = 0;

timeSecs = 0;

}

}

//Now let's make sure that the time is properly displayed

if (timeSecs < 10 && timeMins < 10 && timeHrs < 10)

songCurrentlyAtLabel.setText("+0" + timeHrs +":0" + timeMins +":0" + timeSecs);

elseif (timeSecs >= 10 && timeMins < 10 && timeHrs < 10)

songCurrentlyAtLabel.setText("+0" + timeHrs +":0" + timeMins +":" + timeSecs);

elseif (timeSecs >= 10 && timeMins >= 10 && timeHrs < 10)

songCurrentlyAtLabel.setText("+0" + timeHrs +":" + timeMins +":" + timeSecs);

elseif (timeSecs >= 10 && timeMins >= 10 && timeHrs >= 10)

songCurrentlyAtLabel.setText("+" + timeHrs +":" + timeMins +":0" + timeSecs);

elseif (timeSecs < 10 && timeMins < 10 && timeHrs >= 10)

songCurrentlyAtLabel.setText("+" + timeHrs +":0" + timeMins +":0" + timeSecs);

elseif (timeSecs < 10 && timeMins >= 10 && timeHrs >= 10)

songCurrentlyAtLabel.setText("+" + timeHrs +":" + timeMins +":0" + timeSecs);

songCurrentlyAtLabel.repaint();//Doesn't fix the freezing

//Second we need to take care of the negative counter

downTimeSecs--;

if (downTimeSecs == -1)

{

downTimeSecs = 59;

downTimeMins--;

if (downTimeMins == -1)

{

downTimeHrs--;

downTimeMins = 59;

downTimeSecs = 59;

}

}

//Print out the negative timer properly

if (downTimeSecs < 10 && downTimeMins < 10 && downTimeHrs < 10)

songToGoLabel.setText("-0" + downTimeHrs +":0" + downTimeMins +":0" + downTimeSecs);

elseif (timeSecs >= 10 && timeMins < 10 && downTimeHrs < 10)

songToGoLabel.setText("-0" + downTimeHrs +":0" + downTimeMins +":" + downTimeSecs);

elseif (timeSecs >= 10 && timeMins >= 10 && downTimeHrs < 10)

songToGoLabel.setText("-0" + downTimeHrs +":" + downTimeMins +":" + downTimeSecs);

elseif (timeSecs >= 10 && timeMins >= 10 && downTimeHrs >= 10)

songToGoLabel.setText("-" + downTimeHrs +":" + downTimeMins +":0" + downTimeSecs);

elseif (timeSecs < 10 && timeMins < 10 && downTimeHrs >= 10)

songToGoLabel.setText("-" + downTimeHrs +":0" + downTimeMins +":0" + downTimeSecs);

elseif (timeSecs < 10 && timeMins >= 10 && downTimeHrs >= 10)

songToGoLabel.setText("-" + downTimeHrs +":" + downTimeMins +":0" + downTimeSecs);

songToGoLabel.repaint();//Nor does this

//And finally increment the song tracking variable

songTrack++;

if (songTrack == duration)

{

//End song and do whatever

System.out.println("done");

}

}

}

);

Any suggestions? Thanks!

[6668 byte] By [Jason102a] at [2007-10-3 8:43:38]
# 1
Are you using java.util.Timer or javax.swing.Timer?
CaptainMorgan08a at 2007-7-15 3:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

I know I'm not using the util timer because none of my import statements contain a

import java.util.Timer;

or import java.util.*;

I'm pretty sure I'm doing the import part correctly because I do have this:

import javax.swing.*;

Jason102a at 2007-7-15 3:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

you have a lot of conditionals there, might be worth testing the 'time' another way

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class StopWatch

{

java.text.DecimalFormat df = new java.text.DecimalFormat("00");

JLabel lbl = new JLabel(showTheTime());

javax.swing.Timer timer;

int counter = 0;

public void buildGUI()

{

lbl.setFont(new Font("monospaced",Font.BOLD, 20));

JPanel p = new JPanel();

p.add(lbl);

JFrame f = new JFrame();

f.getContentPane().add(p);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

startTheClock();

}

public void startTheClock()

{

ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent ae){

lbl.setText(showTheTime());

}

};

timer = new javax.swing.Timer(1000,al);

timer.start();

}

private String showTheTime()

{

int hours = counter/3600;

int mins = (counter%3600)/60;

int secs = counter%60;

counter++;

return df.format(hours)+":"+df.format(mins)+":"+df.format(secs);

}

public static void main(String[] args){new StopWatch().buildGUI();}

}

Michael_Dunna at 2007-7-15 3:52:19 > top of Java-index,Desktop,Core GUI APIs...
# 4
DecimalFormat! Why didn't I think of using that before? I still don't get why the code I have freezes when it does, but I'll test this better alternative for a while and see what happens. I'll post again if I keep having this problem. Thanks Michael!
Jason102a at 2007-7-15 3:52:19 > top of Java-index,Desktop,Core GUI APIs...