Hi. I'm trying to create a program for work that has a timer. I've tried it as many ways as i could think of, and the timer itself works great, but i wanted the timer to change a label on a GUI form. However, it won't change the text property of a label in the middle of a function. Is there any way around this. Here is how my code is currently laid out:
When the button is clicked:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String label = new String("jLabel2.text");
jLabel1.setText("Taken");
SeatTimer timer1 = new SeatTimer();
jLabel2.setText("60:00");
while(jLabel2.getText().compareTo("00:00")!=0)
{
jLabel2.setText(timer1.decTime());
}
jLabel1.setText("Open");
}
The logic:
import javax.swing.Timer;
import java.awt.event.*;
import javax.swing.*;
import Logic.*;
public class SeatTimer
{
private final int DELAY = 1;
private int minF = 0;
private int minS = 0;
private int secF = 0;
private int secS = 0;
private String result = new String();
public SeatTimer() {
minF=6;
minS=0;
secF=0;
secS=0;
}
public String decTime()
{
int delay = 100;//milliseconds
result = minF+minS+":"+secF+secS;
result = dec();
StopWatch s = new StopWatch();
s.start();
while(s.getElapsedTimeSecs()<1){}
s.stop();
System.out.println(result);
return result;
}
public String dec()
{
if(secS>0)
{
secS--;
}
else
{
if(secF>0)
{
secF--;
secS=9;
}
else
{
if(minS>0)
{
minS--;
secF=5;
secS=9;
}
else
{
if(minF>0)
{
minF--;
minS=9;
secF=5;
secS=9;
}
else
{
return "Expired";
}
}
}
}
String result = minF+""+minS+":"+secF+""+secS;
return result;
}
}
StopWatch Class:
public class StopWatch
{
private long startTime = 0;
private long stopTime = 0;
private boolean running = false;
public void start() {
this.startTime = System.currentTimeMillis();
this.running = true;
}
public void stop() {
this.stopTime = System.currentTimeMillis();
this.running = false;
}
public long getElapsedTime() {
long elapsed;
if (running) {
elapsed = (System.currentTimeMillis() - startTime);
}
else {
elapsed = (stopTime - startTime);
}
return elapsed;
}
public long getElapsedTimeSecs() {
long elapsed;
if (running) {
elapsed = ((System.currentTimeMillis() - startTime) / 1000);
}
else {
elapsed = ((stopTime - startTime) / 1000);
}
return elapsed;
}
}
There it is. Any help you guys can offer would be great. Thanks in advance.