- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个简单的控件来浏览和采样音频文件。我想使用ObjectProperty<File>
这样我就可以绑定(bind)负责播放文件的按钮的一些属性:
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 here 中撕下了代码,但是当我使用 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
。我知道它确实如此,因为切换按钮变为 Activity 状态并按下它会播放声音文件。
那么我在这里错过了什么/做错了什么?
我想我可能有一个想法:我重写的方法之一是 set 方法:
@Override public void set(File newValue){
if (newValue != null && newValue.exists())
super.set(newValue);
else super.set(null);
}
是否重写 set 方法会导致它不触发重写的 asString
方法?
最佳答案
问题是每次asString()
时您都会创建一个新的绑定(bind)方法被调用。由于您第一次调用它时文件是 null
,您将获得 Bindings.format("[NONE]", this)
创建的绑定(bind)。所以你在按钮上的绑定(bind)相当于:
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);
另一种说法是,检查绑定(bind)中是否存在您想要的名称的有效文件的逻辑不存在,它位于 asString()
中。方法。该方法不会仅仅因为属性更改而被调用。
因此您需要创建一个 StringBinding
当它是 get()
时处理所有逻辑(检查文件是否为空,如果不存在则检查它是否存在,如果存在则获取名称等)方法被调用。您可以通过子类化 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 ;
}
}
就其值(value)而言,我倾向于选择一种除非必要否则避免子类化的风格。在这种情况下,我会将 StringBinding
仅绑定(bind)到文件属性的单独对象。这里的选择取决于用例,但这适用于大多数用例,并且您永远不会发现自己问“我的重写方法是否以一种不起作用的方式交互”,并且一般来说,像您遇到的那样的错误更多这种风格很明显:
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);
.
关于java - 如何正确地将 .Text 属性绑定(bind)到 SimpleObjectProperty 的 Overridden .asString() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638445/
我在 Web 应用程序中尝试了一些字节码操作,到目前为止,效果很好。现在我需要在代码中的某些特定位置注入(inject)一些字节代码。我用 switch 语句和 method.inserAt(); 尝
我正在尝试对数组中的对象值求和,然后计算每个值相对于总数的百分比。 这里是数组: [ { "ratiototale": [ [ { "0":
我在接口(interface)中有以下方法.. Task> SearchAsync(TU searchOptions); 效果很好。 现在我正在尝试进行单元测试以测试何时出现问题 - 代码会抛出异常。
我似乎无法让 reveal 开始工作,我已经严格遵守所有内容,添加 modernizr,添加插件,添加 css(reveal 显然包含在 foundation 中),在开始时初始化 foundatio
我正在尝试移植 raspberrypi's userspace 的位从 C 到 golang 的代码,我遇到了一个涉及 ioctl() 的程序。 我在执行 C 代码时遇到了问题 #define MAJ
我一直在尝试用纯 Java 制作一个简单的游戏,但在绘图时遇到了问题。我正在尝试保持相对较高的帧速率,但是 JFrame.repaint() 不能被“强制”并且只是在下一个可用机会重绘帧的请求这一事实
给定一个字符串 "2*(i+j) = 20 我想返回 ((lambda x: x >= 20), ['i']) 我可以然后直接提供给 constraint。 最佳答案 您正在寻找 eval 的替代方法
我对改进我的 javascript 代码以使其成为正确的 OOP 很感兴趣....目前我倾向于做这样的事情: jQuery(document).ready(function () { Page
我有一个调用两个动画 Action 的事件监听器。不幸的是,它们的开始有少量错开(例如,函数中的第一个首先开始)。 有谁知道正确同步它们的方法吗? 这是我的代码: $("#nav ul li a").
我只需要检查目录是否存在!但是,如果目录是“E:\Test”,其中 E: 是 CD/DVD 驱动器,并且上面没有插入磁盘,我会看到以下 Delphi 和 Windows 问题。 第一种方法: func
同样的问题: https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my
如果您跳转到 this question 中的第一个答案你会看到他使用 Employee.prototype = new Person(); 将 Person.prototype 继承到 Employ
我需要知道如何正确地遍历元素的 ArrayList 并计算元素在列表中出现的次数,而无需事先知道该元素。我尝试了几种方法并且有一种目前有效,但我觉得它很丑陋且不合适。 为了更深入地解释,我有一个 ja
我有一个用 Python 编写的(非常基本但工作完美的)AWS lambda 函数,但是它具有嵌入式凭证以连接到:1)外部网络服务2) DynamoDB 表。 该函数的作用相当基本:它针对服务发布登录
我很好奇 Tornado 推荐的查询 Redis(或任何数据库)的方法是什么。 我见过一些像 https://gist.github.com/357306 这样的例子但他们似乎都在使用对 redis
这更像是一个“我做得对吗”的问题。 快速背景故事:我已经构建了一个 gradle 插件(在一个独立的 gradle/groovy 项目中)。我在另一个 java 项目中使用它。客户项目通过以下方式引用
在我的代码中,我有: function handleMessage() { const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_
我正在努力在 Linux 中刷新嵌入式设备的先前 ROM 转储。我以前的转储包含 oob 数据。我是用nandwrite -n -N -o/dev/mtd0 backup.bin写的,然后再做一次RO
我正在尝试使用 go 为 react-router 提供服务,我已经做到了,但我遇到了麻烦,我认为我做的方式不正确,或者不完整。我正在使用 Mux。我遇到的麻烦是当我按下 时在我的应用程序中,它正确
我正在尝试 promise JSON.parse 方法,但不幸的是没有任何运气。这是我的尝试: Promise.promisify(JSON.parse, JSON)(data).then((resu
我是一名优秀的程序员,十分优秀!