JavaFX将列表大小绑定到this.setTitle()。

huangapple go评论131阅读模式
英文:

JavaFX bind list size to this.setTitle()

问题

以下是翻译好的内容:

  1. 我希望我的笔记程序的标题在我的列表笔记更改时也随之改变为了实现这一点我想将一个IntegerProperty绑定到我的列表的大小但是它显示
  2. > Property<Number> 类型中的 bind(ObservableValue<? extends Number>) 方法不适用于参数 (int)
  3. 这是否意味着我应该将大小从int转换为Number我尝试过但出现了另一个问题),还是有更简单的解决方案
  4. public class Notes extends Stage {
  5. ObservableList<String> notes = FXCollections.observableArrayList();
  6. public Notes() {
  7. this.setup();
  8. }
  9. private void setup() {
  10. IntegerProperty size = new SimpleIntegerProperty();
  11. size.bind(this.notes.size());
  12. this.setTitle(String.format("Notes (%d)", size.getValue()));
  13. final Scene scene = new Scene(this.createRootPane());
  14. this.setScene(scene);
  15. }
  16. }
英文:

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?

  1. public class Notes extends Stage {
  2. ObservableList&lt;String&gt; notes = FXCollections.observableArrayList();
  3. public Notes() {
  4. this.setup();
  5. }
  6. private void setup() {
  7. IntegerProperty size = new SimpleIntegerProperty();
  8. size.bind(this.notes.size());
  9. this.setTitle(String.format(&quot;Notes (%d)&quot;, size.getValue()));
  10. final Scene scene = new Scene(this.createRootPane());
  11. this.setScene(scene);
  12. }
  13. }

答案1

得分: 1

只需将您的标题属性绑定到 列表大小asString 绑定上:

  1. titleProperty().bind(Bindings.size(notes).asString("Notes (%d)"));
英文:

Just bind your title property to the list size’s asString binding:

  1. titleProperty().bind(Bindings.size(notes).asString(&quot;Notes (%d)&quot;));

huangapple
  • 本文由 发表于 2020年10月26日 23:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64540572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定