英文:
JavaFX bind list size to this.setTitle()
问题
以下是翻译好的内容:
我希望我的笔记程序的标题在我的列表(笔记)更改时也随之改变。为了实现这一点,我想将一个IntegerProperty绑定到我的列表的大小,但是它显示:
> Property<Number> 类型中的 bind(ObservableValue<? extends Number>) 方法不适用于参数 (int)
这是否意味着我应该将大小从int转换为Number(我尝试过,但出现了另一个问题),还是有更简单的解决方案?
public class Notes extends Stage {
ObservableList<String> notes = FXCollections.observableArrayList();
public Notes() {
this.setup();
}
private void setup() {
IntegerProperty size = new SimpleIntegerProperty();
size.bind(this.notes.size());
this.setTitle(String.format("Notes (%d)", size.getValue()));
final Scene scene = new Scene(this.createRootPane());
this.setScene(scene);
}
}
英文:
I want the title of my notes-program to change whenever my list (notes) changes. To achieve this I wanted to bind an IntegerProperty to the size of my list, but it says:
>The method bind(ObservableValue<? extends Number>) in the type Property<Number> is not applicable for the arguments (int)
Does this mean I should cast the size from int to a Number (tried it but there was another problem) or is there an even easier solution?
public class Notes extends Stage {
ObservableList<String> notes = FXCollections.observableArrayList();
public Notes() {
this.setup();
}
private void setup() {
IntegerProperty size = new SimpleIntegerProperty();
size.bind(this.notes.size());
this.setTitle(String.format("Notes (%d)", size.getValue()));
final Scene scene = new Scene(this.createRootPane());
this.setScene(scene);
}
}
答案1
得分: 1
只需将您的标题属性绑定到 列表大小 的 asString 绑定上:
titleProperty().bind(Bindings.size(notes).asString("Notes (%d)"));
英文:
Just bind your title property to the list size’s asString binding:
titleProperty().bind(Bindings.size(notes).asString("Notes (%d)"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论