[英]How to bind a StringProperty to a BigDecimal?
I have a ChangeListener
that when triggered, a findGPA()
method is called. 我有一个
ChangeListener
,当触发时,将findGPA()
方法。
private void findGPA(){
GPA = gradeCre/sum;
decGPA = new BigDecimal(GPA);
decGPA = decGPA.setScale(2, RoundingMode.CEILING);
System.out.println("Your average GPA is: " + decGPA);
}
The BigDecimal
is initialized as follows: BigDecimal
的初始化如下:
private BigDecimal decGPA = BigDecimal.ZERO;
In the initialize()
method: 在
initialize()
方法中:
SubmitStageBorderPane.setBottom(AddStackCircle());
which calls: 哪个调用:
private StackPane AddStackCircle(){
StackPane stackCircle = new StackPane();
StringProperty gpa = new SimpleStringProperty("");
gpa.bind(new SimpleStringProperty(decGPA.toString()));
Text avgGPA = createText("Your semester GPA is: " + gpa);
Circle resultCircle = createCircle(avgGPA);
stackCircle.getStyleClass().add("stackCircle");
stackCircle.getChildren().addAll(resultCircle, avgGPA);
return stackCircle;
}
private Circle createCircle(Text avgGPA){
Circle resultCircle = new Circle();
resultCircle.setFill(Color.GREEN);
resultCircle.setStroke(Color.GREY);
resultCircle.setStrokeWidth(3);
resultCircle.setRadius(getWidth(avgGPA) / 2 + 10);
return resultCircle;
}
private Text createText(String text){
Text avgGPA = new Text(text);
avgGPA.setBoundsType(TextBoundsType.VISUAL);
avgGPA.getStyleClass().add("avgGPA");
return avgGPA;
}
However, when i run it, it produces the following label text Your semester GPA is: StringProperty[bound, invalid]
, and it doesn't change even when the value of decGPA
changes. 但是,当我运行它时,它将生成以下标签文本,
Your semester GPA is: StringProperty[bound, invalid]
,即使decGPA
的值更改,它也不会更改。
For the text to update, you'd need to bind the text of the Text
node instead of simply setting the text. 对于文本进行更新,你需要将文本绑定
Text
节点的简单设置的文字。 Furthermore you should bind it to a property that is actually updated, not just a property that will never be modidfied (like new SimpleStringProperty(decGPA.toString())
). 此外,您应该将其绑定到实际更新的属性,而不仅仅是永远不会被修改的属性(例如
new SimpleStringProperty(decGPA.toString())
)。
Assuming the findGPA
method is properly called, it should be implemented like this: 假设
findGPA
方法被正确调用,则应按以下方式实现:
private final ObjectProperty<BigDecimal> decGPA = new SimpleObjectProperty(BigDecimal.ZERO);
private void findGPA(){
GPA = gradeCre/sum;
decGPA.set(BigDecimal.valueOf(GPA).setScale(2, RoundingMode.CEILING));
System.out.println("Your average GPA is: " + decGPA.get());
}
private Text createText(ObservableValue<String> textExpression){
Text avgGPA = new Text();
avgGPA.textProperty().bind(textExpression);
avgGPA.setBoundsType(TextBoundsType.VISUAL);
avgGPA.getStyleClass().add("avgGPA");
return avgGPA;
}
Text avgGPA = createText(decGPA.asString("Your semester GPA is: %s"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.