我正在创建一个简单的控件来浏览和采样音频文件。我想使用ObjectProperty<File>
,这样我就可以绑定负责播放文件的按钮的一些属性:
PlayButton.disableProperty.bind(this.BGMFile.isNull());
PlayButton.textProperty.bind(this.BGMFile.asString());
因此,我需要推翻三件事,其中两件我已经成功完成,因此不会进入
第三种是asString方法:
new SimpleObjectProperty<File>(this, "BGM File", null){
/*yadda yadda overrides*/
@Override public StringBinding asString(){
if (super.get() != null && super.get().exists())
return (StringBinding) Bindings.format(
super.get().getName(), this
);
else return (StringBinding) Bindings.format("[NONE]", this);
}
}
这对我来说是正确的,我甚至在这里从grepCode中撕下了代码,但当我使用FileChooser浏览文件时,我已经设置并选择了要使用的文件,然后将其设置为SimpleProperty,按钮文本保持为[NONE]。
这是浏览文件的代码:
this.btnBrowseBGM.setOnAction((ActionEvent E) -> {
FileChooser FC = new FileChooser();
FC.getExtensionFilters().add(Filters.AudioExtensions());
FC.setTitle("Browse for Background Audio File");
File F = FC.showOpenDialog(this.getScene().getWindow());
if (F != null && F.exists()) try {
this.BGMFile.set(Files.copy(
F.toPath(),
Paths.get("Settings/Sound/", F.getName()),
StandardCopyOption.REPLACE_EXISTING
).toFile());
} catch(IOException ex) {
Methods.Exception(
"Unable to copy file to Settings Sound Directory.",
"Failed to copy Sound File", ex);
this.BGMFile.set(F);
} else this.BGMFile.set(null);
E.consume();
});
因为路径不存在,它会对我大喊大叫(这是我所期望的),但它仍然应该将BGMFile
属性设置为F
。我知道这是因为切换按钮变为活动状态,按下它可以播放声音文件。
那么,我在这里错过了什么/做错了什么?
编辑:
我想我可能有个主意:我覆盖的方法之一是设置方法:
@Override public void set(File newValue){
if (newValue != null && newValue.exists())
super.set(newValue);
else super.set(null);
}
会不会是覆盖set方法导致它没有触发被覆盖的asString
方法?
问题是每次调用asString()
方法时都会创建一个新的Binding。由于第一次调用它是在文件为null
时,因此您将获得由Bindings.format("[NONE]", this)
创建的绑定。所以你在按钮上的绑定相当于:
playButton.textProperty().bind(Bindings.format("[NONE]", bgmFile));
因此,即使在文件属性更改时重新评估字符串值,它仍然会格式化"[NONE]"
。
如果你做,你可以看到相反的问题
ObjectProperty<File> fileProperty = new SimpleObjectProperty<File>() {
/* your previous implementation */
};
fileProperty.set(new File("/path/to/some/valid/file"));
// now bind when you get the filename:
playButton.textProperty().bind(fileProperty.asString());
// setting the fileProperty to null will now invoke the binding that was provided when it wasn't null
// and you'll see a nice bunch of null pointer exceptions:
fileProperty.set(null);
另一种说法是,检查是否有您想要的名称的有效文件不存在于绑定中的逻辑,它在asString()
方法中。该方法不会仅仅因为属性更改而被调用。
因此,当调用get()
方法时,您需要创建一个单独的StringBinding
来处理所有逻辑(检查文件是否为null,如果不检查文件是否存在,如果存在则获取名称,等等)。您可以通过子类化StringBinding
并将逻辑放在computeValue()
方法中,或者使用如下所示的实用程序Bindings.createStringBinding(...)
方法来完成此操作:
new SimpleObjectProperty<File>(this, "BGM File", null){
final StringBinding string = Bindings.createStringBinding(() -> {
File file = this.get();
if (file != null && file.exists()) {
return file.getName();
} else {
return "[NONE]";
}
}, this);
@Override public StringBinding asString(){
return string ;
}
}
就其价值而言,我倾向于选择一种风格,在这种风格中,除非必要,否则我会避免子类化。在这种情况下,我会使StringBinding
成为一个单独的对象,它只绑定到文件属性。这里的选择取决于用例,但这适用于大多数用例,你永远不会发现自己在问"我的重写方法是否以不起作用的方式进行交互",通常情况下,像你遇到的错误在这种风格中更明显:
ObjectProperty<File> bgmFile = new SimpleObjectProperty(this, "bgmFile", null);
StringBinding fileName = Bindings.createStringBinding( () -> {
File file = bgmFile.get();
if (file != null && file.exists()) {
return file.getName();
} else return "[NONE]";
}, bgmFile);
然后当然只做playButton.textProperty().bind(fileName);
。