Reputation: 121
i created a program to show the result of gcd(greatest common divider) between 2 integer numbers step by step not just the final result in java fx
here is the controller class for it
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
int q, r1, r2, r3, r4, r;
private Main main;
private Stage primaryStage;
public void setMain(Main main, Stage primaryStage) {
this.main = main;
this.primaryStage = primaryStage;
}
@FXML
private Label label;
@FXML
private TextFlow tf;
@FXML
private TextArea ta;
@FXML
private Text t;
@FXML
private TextField field;
@FXML
private TextField field1;
@FXML
private void handleButtonAction(ActionEvent event) {
String t1 = field.getText();
String t2 = field1.getText();
if (t1.isEmpty() || t2.isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText("Enter Values");
alert.setContentText("you have to enter Integer Numbers");
alert.showAndWait();
} else {
try {
r1 = Integer.parseInt(t1);
r2 = Integer.parseInt(t2);
if (r1 < r2) {
r3 = r2;
r4 = r1;
r = r3 % r4;
q = r3 / r4;
String t = ("r1=" + r3 + " " + "r2=" + r4 + " " + "q=" + q + " " + "r=" + r);
String t6 = new String();
while (r != 0) {
r3 = r4;
r4 = r;
r = r3 % r4;
q = r3 / r4;
t6 = "r1=" + r3 + " " + "r2=" + r4 + " " + "q=" + q + " " + "r=" + r;
System.out.println(t6);
}
String t7 = ("gcd=" + r4);
label.setText(t + "\n" + t6 + "\n" + t7 + "\n");
label.setWrapText(true);
} else {
r = r1 % r2;
q = r1 / r2;
String t3 = ("r1=" + r1 + " " + "r2=" + r2 + " " + "q=" + q + " " + "r=" + r);
String t4 = new String();
//this part of code
while (r != 0) {
r1 = r2;
r2 = r;
r = r1 % r2;
q = r1 / r2;
t4 = ("r1=" + r1 + " " + "r2=" + r2 + " " + "q=" + q + " " + "r=" + r);
}
String t5 = ("gcd=" + r2);
label.setText(t3 + "\n" + t4 + "\n" + t5 + "\n");
}
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText("Values Error");
alert.setContentText("you can only enter Integer Numbers");
alert.showAndWait();
}
field.clear();
field1.clear();
}
}
public void close(){
primaryStage.close();
}
@Override
public void initialize(URL url, ResourceBundle rb){}
public void changeWindow(){
main.secondWindow();
}
}
now here is the result when i enter for example 26 15 here is the result
r1=26 r2=15 q=1 r=11
r1=3 r2=1 q=3 r=0
gcd=1
but the result should be like this as is it apear in the terminal
r1=26 r2=15 q=1 r=11
r1=15 r2=11 q=1 r=4
r1=11 r2=4 q=2 r=3
r1=4 r2=3 q=1 r=1
r1=3 r2=1 q=3 r=0
1
so as i said why the label only show the final result of loop not the whole loop like the terminal show it
Upvotes: 0
Views: 605
Reputation: 82461
If you want to include every step in the Label
, you need to add something to the final result for every loop iteration. You do not do something like this. I recommend using a StringBuilder
for this purpose:
if (r1 > r2) {
int t = r1;
r1 = r2;
r2 = t;
}
StringBuilder sb = new StringBuilder();
while (r1 > 0) {
int temp = r2 % r1;
sb.append("r1=").append(r2).append(" r2=").append(r1).append(" q=").append(r2/r1).append(" r=").append(temp).append('\n');
r2 = r1;
r1 = temp;
}
sb.append("gcd=").append(r2);
label.setText(sb.toString());
Upvotes: 1
Reputation: 15622
The problem is that your logic runs withing the Event Dispatch Thread (EDT). Asl long as your logic runs the EDT cannot update the screen.
You need to move your logic into a separate Threat that calls the setText()
method within Platform.runLater()
Upvotes: 1