I'm creating a simple control to browse for and sample Audio files. I want to use an ObjectProperty<File>
so that I can bind some properties of the button responsible for playing the file:
PlayButton.disableProperty.bind(this.BGMFile.isNull());
PlayButton.textProperty.bind(this.BGMFile.asString());
So then there will be three things that I will need to override, two of which I have successfully done, and so will not get into
The third is the asString method:
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);
}
}
This feels correct to me, and I even ripped the code from grepCode here, but when I browse for a file using the FileChooser I have setup and select the file I want to use, and then set it to the SimpleProperty the button text stays as [NONE].
This is the code for browsing for the file:
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();
});
Because the path doesn't exist it yells at me (which I expected) but it still should be setting the BGMFile
property to F
. I know it does because the toggle button becomes active and pressing it plays the sound file.
So what am I missing/doing wrong here?
EDIT:
I think I may have an idea: One of the methods which I override is the set method:
@Override public void set(File newValue){
if (newValue != null && newValue.exists())
super.set(newValue);
else super.set(null);
}
Could it be that overriding the set method causes it to not trigger the overridden asString
method?