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!

