aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Updating GUI properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating GUI properties" Watch "Updating GUI properties" New topic
Author

Updating GUI properties

Daniel Layher
Greenhorn

Joined: Jul 14, 2006
Posts: 19
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

Joined: Mar 26, 2002
Posts: 442
Have you had a look at SwingUtilities.invokeLater() & invokeAndWait yet?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Moving to our Swing related forum...


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use swing's timer

simple demo

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating GUI properties