小编a3p*_*2.0的帖子

JavaFX:使用常量字符串前缀绑定StringProperty

我对JavaFX中的绑定功能有疑问.我想要的是绑定2个字符串属性.但他们的价值观不应该相等.

让我举一个例子:

我有一个StringProperty代表我的应用程序中最后打开的项目.
值类似于"C:\ temp\myProject.prj".
我想在窗口标题中显示此路径.
这很简单:stage.titleProperty().bind(lastprojectProperty());
但我不想只显示项目路径,还要显示应用程序名称,
例如:MyApplication 2.2.4 - C:\ temp\myProject.prj.

可以使用绑定并添加一个常量前缀字符串吗?或者我是否使用ChangeListerner?

使用ChangeListener的解决方案存在初始值的问题......

    final StringProperty path = new SimpleStringProperty("untitled");
    final StringProperty title = new SimpleStringProperty("App 2.0.0");

    path.addListener(new ChangeListener<String>()
  {
        @Override
        public void changed(ObservableValue<? extends String> ov, String t, String newValue)   
        {
            title.setValue("App 2.0.0 - " + newValue);
        }
  });                

    // My title shows "App 2.0.0" since there is now change event throws until now...
    // Of course I could call path.setValue("untitled"); 
    // And above path = new …
Run Code Online (Sandbox Code Playgroud)

bind javafx properties changelistener

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

bind ×1

changelistener ×1

javafx ×1

properties ×1