- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在看这个问题JavaFX show dialogue after thread task is completed ,但我的问题有点相反。在文件选择器或警报之后需要从用户返回一些数据时,最好的线程关闭方法是什么?
这是我现在拥有的:
Platform.runLater(()->{
File file = fileChooser.showOpenDialog(root.getScene().getWindow());
if(file == null) {
return;
}
executorService.execute(()->{
//more code here which uses file
});
});
其中 executorService 是之前创建的 ExecutorService。我想我可以轻松地使用任务或线程或其他任何东西,但它如何线程化并不重要,只是这需要一段时间,我不希望在应用程序线程上发生,因为它会锁定 UI。
我知道这不是 mvce,但我希望它能演示我在 Platform.runLater
调用中遇到的线程问题。
这是一个极端的例子,说明这种事情是多么复杂
@FXML
public void copyFiles(ActionEvent event){
//this method is on the application thread because a button or something started it
// so we thread off here
executorService.execute(()->{
// do some stuff
// ...
// get location to copy to from user
// must happen on the application thread!
Platform.runLater(()->{
File file = fileChooser.showOpenDialog(root.getScene().getWindow());
if(file == null) {
return;
}
executorService.execute(()->{
// more code here which uses file
// ...
// oh wait, some files have the same names!
// we need a user's confirmation before proceeding
Platform.runLater(()->{
Alert alert = new Alert(AlertType.CONFIRMATION, "Do you want to overwrite files with the same names?", ButtonType.OK, ButtonType.CANCEL);
Optional<ButtonType> choice = alert.showAndWait();
if(choice.isPresent && choice.get == ButtonType.OK){
// do something, but not on the application thread
executorService.execute(()->{
// do the last of the copying
// ...
});
}
});
});
});
});
}
最佳答案
如果您需要在返回结果的 UI 线程上执行某些操作,请创建一个 FutureTask
,将其提交到 UI 线程,然后在后台线程上等待它完成。这允许您“扁平化”代码。
您还可以将 Platform.runLater(...)
抽象为 Executor
(毕竟,它只是执行 Runnable
的东西) s),这可以使它(也许)稍微干净一些。
通过分成更小的方法(通常只使用其他标准编程技术),您可以使代码变得非常干净。
这是基本思想(您需要添加异常处理,或创建一个 Callable
(可以引发异常)而不是 Runnable
):
@FXML
public void copyFiles(ActionEvent event){
Executor uiExec = Platform::runLater ;
//this method is on the application thread because a button or something started it
// so we thread off here
Callable<Void> backgroundTask = () -> {
doFirstTimeConsumingThing();
FutureTask<File> getUserFile = new FutureTask<>(this::getUserFile) ;
uiExec.execute(getUserFile);
File file = getUserFile.get();
if (file == null) return null ;
doAnotherTimeConsumingThing(file);
FutureTask<Boolean> getUserConfirmation = new FutureTask<>(this::showConfirmation);
uiExec.execute(getUserConfirmation);
if (! getUserConfirmation.get()) return null ;
doMoreTimeConsumingStuff();
// etc...
return null ;
};
executorService.execute(backgroundTask);
}
private File getUserFile() {
return fileChooser.showOpenDialog(root.getScene().getWindow());
}
private Boolean getUserConfirmation() {
Alert alert = new Alert(AlertType.CONFIRMATION, "Do you want to overwrite files with the same names?", ButtonType.OK, ButtonType.CANCEL);
return alert.showAndWait()
.filter(ButtonType.OK::equals)
.isPresent();
}
private void doFirstTimeConsumingThing() {
// ...
}
private void doAnotherTimeConsumingThing(File file) {
// ....
}
private void doMoreTimeConsumingStuff() {
// ...
}
关于java - 如何正确关闭 javafx Alerts/fileChooser 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62012011/
我如何在 HP Fortify SSC 上定义警报,只有在发现新问题或自上次扫描以来问题数量增加时才会发出警报。 我可以定义一个包含静态问题数量的警报,但每次警报数量发生变化时都需要手动更新,我想避免
由于未知原因,我的 Alert.alert 拒绝工作。我基本上是在重用之前工作的代码。获取错误: Exception NSArrayl length; unrecognized selector se
我的注销用户功能似乎根本不起作用。我在登录时通过警报调用它,但它似乎没有调用它。如果我尝试在末尾添加“()”,它只会给我一个错误。 loginUser = (email, password) => {
alert() 和 window.alert() 函数有什么区别?看起来效果一样。 最佳答案 因为 window 是全局对象,您可以通过简写调用 alert:alert( 'Hello!' ); 或通
我正在尝试整理此错误消息: Exception '-[_NSDisctionary0 length]: unrecognized selector sent to instance 0x7896412
有什么方法可以改变 JavaScript 中alert 或prompt 的外观吗?诸如添加图像、更改字体颜色或大小之类的事情,以及任何会使它看起来不同的事情。 最佳答案 扩展 Matthew Abbo
我正在尝试创建一个警报,以确保用户提交了正确的信息,如果单击“确定”而不是取消,则单击链接并 发送。我几乎已经实现了,警报激活,但如果单击确定则不会激活。不幸的是,我还不是 js 向导...... 编
看起来 AngularJS $window.alert() 和 Javascript alert() 是一样的。在什么条件下我们应该使用其中的哪一个?还是根本没有区别? 最佳答案 这是一回事——Jav
我的应用需要在不同时间向用户显示一些信息。我决定使用 AlertControllers 但我不能同时显示两个 Alert Controllers。因此我需要知道是否显示了警报 Controller ,
在 Brad's tutorial Alerts 组件使用以下方式导出: export default withAlert(Alerts) 这导致了错误: The above error occurr
我正在使用 Twitter Bootstrap 3 并使用 jQuery AJAX 发送表单数据。这是我的代码: $.ajax({ type: $(form).attr('method
我遇到了 的问题(SSL 警报编号 46) 140097325019584:error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certif
我正在尝试使用 Alert React Native 中的组件以在 Android 和 iOS 之间创建一致的体验。我正在尝试运行示例警报。我导入了警报组件(为简洁起见省略了其他导入): import
考虑这段代码: var input = document.getElementById("hello"); input.addEventListener('blur', function() {
请检查代码, import { Alert, } from 'react-native'; checkForSendingOtp = () => { let hash = 'aBcDeG
我刚开始学习和练习 React Native,我遇到了第一个我自己似乎无法解决的问题。 我有以下代码,非常简单,但是当我在网络上运行时 Alert.alert() 不起作用。如果我单击该按钮,则没有任
在 Safari 浏览器中遇到一个问题,以下是我的场景(带示例)。 当我点击删除帐户的按钮时,我会打开警告消息。在该警报窗口中有两个操作“确定”和“取消”。如果我单击"is",它将重定向到另一个 UR
使用 Cordova CLI 版本 5.4.1,平台是iOS,在 iOS 模拟器上运行 来 self 应用的 onDeviceReady处理程序,我正在调用一个函数来设置一个 Hook ,以便使用 n
我正在使用 selenium IDE。我需要验证在成功填写数据并单击保存按钮后显示的闪现消息。 我正在使用 assertText css=div.alert.alert-success × Succe
我有一个 .pfx 文件,在 Windows 客户端上使用时可以完美连接到远程服务器。我现在想使用 Linux 客户端连接到服务器。 问题 1) 我使用以下 openssl 命令从 pfx 文件中提取
我是一名优秀的程序员,十分优秀!