[英]JavaFX Bind Label textProperty to multiple Properties changes
What i want to do is: 我想做的是:
I have a JavaFX Window which i am constantly changing the width and height of it by dragging the corners or the sides
. 我有一个JavaFX窗口,我通过拖动corners or the sides
不断更改其宽度和高度。 I want when the width or height are changing
then the text of the Label
to have the below format: I want when the width or height are changing
时, Label
的文本具有以下格式:
~Width=[here the width of the Window],Height[here the height of the Window]~
Example: 例:
~Width=[1300],Height=[600]~
I want to do this using bindings and not using 2(two) ChangeListeners
. 我想使用绑定而不是2( ChangeListeners
) ChangeListeners
来做到这一点 。
I am trying:
label.textProperty().bind(I am stack here on how to do this...);
I had read also this question JavaFX bind to multiple properties 我也读过这个问题JavaFX绑定到多个属性
Just use Bindings.createStringBinding
with both width
and height
as dependencies: 只需将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);
I think the easiest way is to use a change listener, and query the with/height values manually. 我认为最简单的方法是使用更改侦听器,并手动查询with / height值。 Like 喜欢
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.