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

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

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&lt;String&gt; notes = FXCollections.observableArrayList();

    public Notes() {
        this.setup();
    }

    private void setup() {
        IntegerProperty size = new SimpleIntegerProperty();
        size.bind(this.notes.size());

        this.setTitle(String.format(&quot;Notes (%d)&quot;, 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(&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:

确定