Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Updating GUI properties

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you had a look at SwingUtilities.invokeLater() & invokeAndWait yet?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our Swing related forum...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use swing's timer

simple demo

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic