[英]JavaFX: Bind the textProperty from a Label to the size of an ObservableList
[英]JavaFX Bind Label textProperty to multiple Properties changes
我想做的是:
我有一个JavaFX窗口,我通过拖动corners or the sides
不断更改其宽度和高度。 I want when the width or height are changing
时, Label
的文本具有以下格式:
~Width=[here the width of the Window],Height[here the height of the Window]~
例:
~Width=[1300],Height=[600]~
我想使用绑定而不是2( ChangeListeners
) ChangeListeners
来做到这一点 。
I am trying:
label.textProperty().bind(I am stack here on how to do this...);
我也读过这个问题JavaFX绑定到多个属性
只需将Bindings.createStringBinding
与width
和height
作为依赖项:
StringBinding binding = Bindings.createStringBinding(
() -> MessageFormat.format("~Width=[{0}],Height=[{1}]~", primaryStage.getWidth(), primaryStage.getHeight()),
primaryStage.widthProperty(),
primaryStage.heightProperty());
label.textProperty().bind(binding);
我认为最简单的方法是使用更改侦听器,并手动查询with / height值。 喜欢
public void init()
{
ChangeListener<Number> listener = (obs, ov, nv) -> update();
node.widthProperty().addListener(listener);
node.heightProperty().addListener(listener);
}
public void update()
{
label.setText(String.format("width[%s] height[%s]", node.getWidth()), node.getHeight);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.