简体   繁体   English

如何将StringProperty绑定到BigDecimal?

[英]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.

相关问题 如何将StringProperty绑定到ObjectProperty <Integer> 在JavaFX中? - How to bind StringProperty to ObjectProperty<Integer> in JavaFX? 如何将标签绑定到StringProperty的首字母? - How to bind label to first letter of StringProperty? 如何绑定ObjectProperty <LocalDate> JavaFX中的StringProperty? - How to bind ObjectProperty<LocalDate> to StringProperty in JavaFX? Javafx将标签绑定到StringProperty的位置 - Javafx where to bind labels to StringProperty 将WebView中DOM节点的内容绑定到StringProperty - Bind the content of a DOM node in WebView to StringProperty JavaFX:使用自定义Formatter将LocalDateProperty绑定到StringProperty - JavaFX: bind LocalDateProperty to StringProperty with custom Formatter 在TornadoFX中,如何使用objectBinding将BigDecimal属性绑定到另一个BigDecimal属性? - In TornadoFX, how can I bind BigDecimal properites to another BigDecimal property using objectBinding? 如何在 Spring MVC 中将用户输入与 BigDecimal 对象字段绑定? - How to bind user input with a BigDecimal object field in Spring MVC? Java BigDecimal:如何增强BigDecimal - Java BigDecimal : How to fortmat BigDecimal 如何在javaFX中更改StringProperty类型的最终值? - how to change final value of StringProperty type in javaFX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM