I'm pretty new to Java programming, but I want to make a simple Up or Down counter (based on input values). I've created an FXML application for my code, and I want the counter to update. Let's say I want to count up from 1 to 6, I want the application to show 1, 2, 3, 4, 5, 6. I've managed to do it in the console, but the FXML application seems to pause while it's counting.
Can someone help me with this? Below is all my code:
UpOrDownCounter.java
package upOrDownCounter;
public class UpOrDownCounter {
private int stop;
private int start;
private int counter = 0;
public UpOrDownCounter(int start, int stop){
this.stop = stop;
this.start = start;
if(start > stop) {
counter = start + 1;
}else {
counter = start - 1;
}
if(start == stop) {
throw new IllegalArgumentException("Start and stop cannot be the same value");
}
}
int getCounter() {
return counter;
}
boolean count() {
if(start > stop) {
if(getCounter() != stop) {
counter = counter - 1;
return true;
}
}else {
if(getCounter() != stop) {
counter = counter + 1;
return true;
}
}
return false;
}
public static void main(String[] args) {
UpOrDownCounter cd = new UpOrDownCounter(6, 6);
System.out.println(cd.getCounter());
System.out.println(cd.count());
System.out.println(cd.getCounter());
System.out.println(cd.count());
System.out.println(cd.getCounter());
System.out.println(cd.count());
System.out.println(cd.getCounter());
System.out.println(cd.count());
System.out.println(cd.getCounter());
System.out.println(cd.count());
System.out.println(cd.getCounter());
System.out.println(cd.count());
System.out.println(cd.getCounter());
System.out.println(cd.count());
}
}
UpOrDownCounterController.java
package upOrDownCounter;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class UpOrDownCounterController {
UpOrDownCounter cd;
@FXML TextField startValue;
@FXML TextField stopValue;
@FXML TextField counterOutput;
@FXML
void handleClick() {
String startString = startValue.getText();
int start = Integer.valueOf(startString);
String stopString = stopValue.getText();
int stop = Integer.valueOf(stopString);
cd = new UpOrDownCounter(start, stop);
while(cd.count()) {
try {
System.out.println("Counter: " + String.valueOf(cd.getCounter()));
counterOutput.setText(String.valueOf(cd.getCounter()));
System.out.println(String.valueOf(cd.getCounter()));
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
UpOrDownCounter.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9" fx:controller="upOrDownCounter.UpOrDownCounterController">
<children>
<TextField fx:id="startValue" promptText="Start" />
<TextField fx:id="stopValue" promptText="Stop" />
<Button onAction="#handleClick" mnemonicParsing="false" prefHeight="37.0" prefWidth="196.0" text="Start countdown" />
<TextField fx:id="counterOutput" prefHeight="32.0" prefWidth="196.0" />
</children>
</VBox>