我在这里使用rating feature of ControlsFX 。每次更改评级值时,我都需要更新 TextField 中的值,如屏幕截图所示。是否可以在这里使用javafx.concurrent.Task?我该如何应用它?
这是我的代码:
@FXML
private HBox ratinghbox;
@FXML
private TextField yourratingtxt;
private Rating rating;
@Override
public void initialize(URL url, ResourceBundle rb) {
rating = new Rating();
rating.setPartialRating(true);
ratinghbox.getChildren().add(rating);
yourratingtxt.setText(String.valueOf(rating.getRating()));
}
最佳答案
使用更改监听器
使用 ratingProperty 更新文本字段
rating.ratingProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable,
Number oldValue, Number newValue) {
textField.setText(newValue.toString());
}
});
使用绑定(bind)
您还可以在文本字段和评分之间创建双向绑定(bind)
(仅当您擅长不编辑文本字段的值时)
textfield.setDisable(true)
Bindings.bindBidirectional(textField.textProperty(), rating.ratingProperty(),
new NumberStringConverter());
如果您必须编辑文本字段,请不要使用此
关于java - 将评级值设置为文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009674/