kendel
November 18th, 2009, 09:25 PM
Ok so I am making a gui using netbeans ide. I created thread class that is basically a timer.
public class Counter extends Thread
{
// time starts initially at zero and is counted in seconds
private double milisecond = 0;
private double second = 0;
private double minute = 0;
private double hour = 0;
public Counter(double s, double m, double h, double ms)
{
milisecond = ms;
second = s;
minute = m;
hour = h;
}
Counter()
{
super();
}
/* Overrided -Indicates that a method declaration is intended to override a method declaration in a superclass */
@Override
public void run()
{
while(true)
{
try
{
sleep(0); // 1000ms = 1 second
milisecond++;
if(milisecond == 1000)
{
milisecond = 0;
second++;
}
if(second == 60)
{
second = 0;
minute++;
}
if(minute == 60)
{
minute = 0;
hour++;
}
if(hour == 24)
{
hour = 0;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public double getMilisecond()
{
return milisecond;
}
public double getSecond()
{
return second;
}
public double getMinute()
{
return minute;
}
public double getHour()
{
return hour;
}
}
I also have a main thread class (declared and utilized in the main) which updates the jLabels.
public class UpdateThread extends Thread
{
@Override
public void run()
{
while(true)
{
try
{
jmilisecondDisplay.setText(""+clockTimer.getMilisecond());
}catch(Exception e)
{
e.printStackTrace();;
}
}
}
}
Ok so I am trying to make two buttons, one which starts the counter (which initially gets displayed in the jLabel).
Ok so I put an action performed on a button which starts the clock thread and one which yields it (pause)
The start works fine but if you click it again there is an error, the yield does not work...
private void jstartButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
counter.start();
}
private void jclearButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
counter.yield();
}
What is a better work around for this situation where i can have a start button and a pause button..
public class Counter extends Thread
{
// time starts initially at zero and is counted in seconds
private double milisecond = 0;
private double second = 0;
private double minute = 0;
private double hour = 0;
public Counter(double s, double m, double h, double ms)
{
milisecond = ms;
second = s;
minute = m;
hour = h;
}
Counter()
{
super();
}
/* Overrided -Indicates that a method declaration is intended to override a method declaration in a superclass */
@Override
public void run()
{
while(true)
{
try
{
sleep(0); // 1000ms = 1 second
milisecond++;
if(milisecond == 1000)
{
milisecond = 0;
second++;
}
if(second == 60)
{
second = 0;
minute++;
}
if(minute == 60)
{
minute = 0;
hour++;
}
if(hour == 24)
{
hour = 0;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public double getMilisecond()
{
return milisecond;
}
public double getSecond()
{
return second;
}
public double getMinute()
{
return minute;
}
public double getHour()
{
return hour;
}
}
I also have a main thread class (declared and utilized in the main) which updates the jLabels.
public class UpdateThread extends Thread
{
@Override
public void run()
{
while(true)
{
try
{
jmilisecondDisplay.setText(""+clockTimer.getMilisecond());
}catch(Exception e)
{
e.printStackTrace();;
}
}
}
}
Ok so I am trying to make two buttons, one which starts the counter (which initially gets displayed in the jLabel).
Ok so I put an action performed on a button which starts the clock thread and one which yields it (pause)
The start works fine but if you click it again there is an error, the yield does not work...
private void jstartButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
counter.start();
}
private void jclearButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
counter.yield();
}
What is a better work around for this situation where i can have a start button and a pause button..