0

This is my first time being perplexed by code in a long time. I need to make GUI count down timer for an assignment where it's nothing but a single input field and upon hitting enter it counts down by seconds the number you entered. I need it to display each passing second. I know there is classes I could use to create a timer but being we haven't covered anything like that in this course yet I want to do it without using them. The code I have right now just counts down from the number entered then displays 0 without showing it counting down. I used similar logic in a console app and it counted down fine so I just tried integrating that into this GUI app. I know what I have is sloppy and could be done better, but I am just looking for tips on what would make it work.

Here is my code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Countdown extends Application {

//create border pane
BorderPane pane = new BorderPane();
//create textfield for timer
TextField text = new TextField();
//variable for input
int inTime;
//start time
double start;
//check time
double checkTime;



public static void main(String[] args) {
    launch(args);

}

@Override
public void start(Stage ps) throws Exception {
    //add textfield to pane
    pane.setCenter(text);
    
    //make a scene
    Scene scene = new Scene(pane, 200, 100);
    
    //key press event
    text.setOnKeyPressed(e->{
        
        //single out the enter key
        if(e.getCode().equals(KeyCode.ENTER)){
            //save start time to variable
            long start = System.currentTimeMillis();
            //variable to allow repeat of while loop 
            boolean repeat = true;
            //set variable to user entry
            inTime = Integer.parseInt(text.getText());
            //while loop to keep track of seconds
            while(repeat) {
                
                //while time being displayed is not at zero
                if(inTime != 0){
                    //repeating function to check if a second has passed
                    if(System.currentTimeMillis() - start == 1000) {
                        text.setText("");//clear text in box
                        text.setText(Integer.toString(inTime));//display current countdown time
                        inTime -= 1;//decrement the counter
                        start = System.currentTimeMillis(); //reset start time
                    }
                    
                }
                //when count has reached zero
                else{
                    repeat = false;//stop the while loop
                    text.setText("0");//display zero 
                }
                
            }
            
        }
        
    });
    
    
    
    
    //put together stage
    ps.setScene(scene);
    ps.setTitle("BJO Chapter 16 Exercise 21");
    ps.show();
    
    
}
}
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ben Olson
  • 31
  • 1
  • what exactly are you having issues with? – blurfus Sep 17 '17 at 14:33
  • You're probably flooding the UI thread, so it can't update the UI. Take a look at this: https://stackoverflow.com/questions/20497845/constantly-update-ui-in-java-fx-worker-thread – Kraylog Sep 17 '17 at 14:40

0 Answers0