This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating GUI properties

 
Daniel Layher
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you had a look at SwingUtilities.invokeLater() & invokeAndWait yet?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our Swing related forum...
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use swing's timer

simple demo

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic