Mol*_*ris 3 javafx model-binding
我知道如何绑定属性,但是如何绑定函数的调用呢?
例如:我有一个ObjectProperty指向文件的。现在,我想将路径绑定到其文件夹?如果值ObjectProperty是C:\\user\Desktop\text.txt,接合应指向C:\\user\Desktop。
我以为我可以getParentFile()在绑定内调用。
小智 5
有很多方法可以映射 an ObjectProperty,看看类Bindings。
(所有示例都假设您有一个ObjectProperty<File> file)
Bindings.createObjectBinding(Callable<T> func, Observable... dependencies)
ObjectBinding<File> parent = Bindings.createObjectBinding(() -> {
File f = file.getValue();
return f == null ? null : f.getParentFile();
}, file);
Run Code Online (Sandbox Code Playgroud)Bindings.select(ObservableValue<?> root, String... steps)
ObjectBinding<File> parent = Bindings.select(file, "parentFile");
Run Code Online (Sandbox Code Playgroud)
当file为空时,这将在错误流上打印警告。
您还可以创建自己的映射方法(类似于createObjectBinding):
public static <T,R> ObjectBinding<R> map(ObjectProperty<T> property, Function<T,R> function) {
return new ObjectBinding<R>() {
{
bind(property);
}
@Override
protected R computeValue() {
return function.apply(property.getValue());
}
@Override
public void dispose() {
unbind(property);
}
};
}
Run Code Online (Sandbox Code Playgroud)
并使用它
ObjectBinding<File> parent = map(file, f -> f == null ? null : f.getParentFile());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
980 次 |
| 最近记录: |