如果所有者最小化,则Javafx对话框(警报)不可见
如果所有者最小化,为什么javafx中的对话框(警报)不能正确显示 请看以下代码:如果所有者最小化,则Javafx对话框(警报)不可见,java,javafx,dialog,javafx-8,Java,Javafx,Dialog,Javafx 8,如果所有者最小化,为什么javafx中的对话框(警报)不能正确显示 请看以下代码: public class JavaFxSample2 extends Application { @Override public void start(Stage primaryStage) throws InterruptedException, ExecutionException { primaryStage.setTitle("open alert in 2 seconds"); Pa
public class JavaFxSample2 extends Application {
@Override
public void start(Stage primaryStage) throws InterruptedException, ExecutionException {
primaryStage.setTitle("open alert in 2 seconds");
Pane pane = new Pane();
Button b = new Button("open dialog");
b.setOnMouseClicked(event -> {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Thread.sleep(2000);
return null;
}
};
task.stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == State.SUCCEEDED) {
Alert alert = new Alert(AlertType.INFORMATION, "Hello");
alert.initOwner(primaryStage);
alert.showAndWait();
}
});
Thread thread = new Thread(task);
thread.start();
});
pane.getChildren().add(b);
Scene scene = new Scene(pane, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
公共类JavaFxSample2扩展应用程序{
@凌驾
public void start(Stage primaryStage)抛出InterruptedException、ExecutionException{
primaryStage.setTitle(“2秒内打开警报”);
窗格=新窗格();
按钮b=新按钮(“打开对话框”);
b、 setOnMouseClicked(事件->{
任务=新任务(){
@凌驾
受保护的Void调用()引发异常{
《睡眠》(2000年);
返回null;
}
};
task.stateProperty().addListener((可观察、旧值、新值)->{
if(newValue==State.successed){
警报警报=新警报(AlertType.INFORMATION,“Hello”);
alert.initOwner(primaryStage);
alert.showAndWait();
}
});
线程线程=新线程(任务);
thread.start();
});
pane.getChildren().add(b);
场景=新场景(窗格,300275);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
如果在2秒钟内最小化应用程序,然后再次将其最大化(在2秒钟后),您将看不到对话框,但不知何故它就在那里(在按esc或enter键之前,阶段是锁定的)
如果不最小化阶段,则对话框将正确显示
这是虫子吗?我做错了吗
编辑:
系统是Windows7,Java1.8.0.66
编辑2:
好啊这似乎真的是一个bug:
找到了(可能的)解决方案。
但这真的是一个好的解决方案吗
在显示警报之前,我执行以下命令:
((Stage) alert.getOwner()).setIconified(false);
如果有人有更好的想法,我将删除我的答案。如果您在显示警报对话框之前添加此语句,您将不必按
Esc
或Enter
。这将使您不再冻结应用程序
alert.initModality(Modality.NONE);
DaoAlloyGeneral a=(DaoAlloyGeneral)tabelaAlloy.getSelectionModel().getSelectedItem();
警报警报=新警报(警报类型.确认);
alert.setTitle(DaoCfgIdiomas.getTraducao(“general.Confirmacao”);
alert.setHeaderText(DaoCfgIdiomas.getTraducao(“general.Atencao”);
alert.setContentText(DaoCfgIdiomas.getTraducao(“maintabloy.msgDeletar”)+“”+a.getNome());
警报。可设置大小(真);
alert.getDialogPane().setPrefSize(480320);
可选结果=alert.showAndWait();
如果(result.get()==ButtonType.OK){
DaoAlloyGeneral.deletarAlloy(a.getIdAlloy());
tabelaAlloy.getItems().clear();
tabelaAlloy.setItems(DaoAlloyGeneral.buscaTodos());
}
我不能理解它以前起作用了,现在,停下来。
Oracle jdk1.8.0_111 linux机器
EDIT:我发现了问题,JVM参数-Dcom.sun.javafx.isEmbedded=true
如果没有这个选项,它就可以工作。这里有一个静态方法,嵌入了@之前的提示 它自动执行该过程,然后将父级恢复到其当前的“图标化”状态
public static <T> Optional< T > Dialog_showAndWait( Dialog< T > _dialog )
{
/**
* a) PATCH:
* IF( dialog.Parent is iconified )
* THEN de-iconify the Parent
*/
Boolean isParentIconized = null;
Stage parentStage = ( (Stage) _dialog.getOwner() );
if( parentStage != null )
{
isParentIconized = parentStage.isIconified();
if( isParentIconized )
parentStage.setIconified( false );
}
/**
* b) Process the original function
*/
Optional< T > result = _dialog.showAndWait();
/**
* c) Restore the initial state of the dialog's Parent
*/
if( parentStage != null )
if( isParentIconized )
parentStage.setIconified( isParentIconized );
return result;
}
公共静态可选对话框\u显示和等待(对话框\u对话框)
{
/**
*a)补丁:
*IF(dialog.Parent已图标化)
*然后取消父对象的图标化
*/
布尔值isParentIconized=null;
Stage parentStage=((Stage)u dialog.getOwner());
if(parentStage!=null)
{
isParentIconized=parentStage.Isiconized();
如果(isParentIconized)
parentStage.setIconified(false);
}
/**
*b)处理原始功能
*/
可选result=_dialog.showAndWait();
/**
*c)恢复对话框父级的初始状态
*/
if(parentStage!=null)
如果(isParentIconized)
父级。设置图标化(isParentIconized);
返回结果;
}
是,我现在不必按esc或enter键。但是对话框也没有显示出来。所以答案是不正确的