java - 超时后自动关闭对话框并取消响应

标签 java timer javafx javafx-8

我通过 Oracle 文档的帮助实现了 javaFX-Dialogs
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html

和教程
http://code.makery.ch/blog/javafx-dialogs-official/

到目前为止我已经做了以下事情:
我在对话框窗口中添加了时间线。

public static void idleness(final DialogTemplate template) {
    System.out.println("timeline Started -" + Calendar.getInstance().getTime());
    Timeline idlestage = new Timeline(new KeyFrame(Duration.seconds(15), new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            template.hide();
        }
    }));
    idlestage.setCycleCount(1);
    idlestage.play();

}

现在对话框窗口在 15 秒后隐藏,但无法获得任何对话框响应。

期望: 如果用户在给定时间内没有给出任何响应,则必须记录否定响应并关闭对话框窗口。

最佳答案

直接设置结果就可以了。

@Override
public void start( Stage stage )
{
    Scene scene = new Scene( new Group(), 200, 300 );

    Alert alert = new Alert( Alert.AlertType.CONFIRMATION );
    alert.setTitle( "Confirmation Dialog" );
    alert.setHeaderText( "Look, a Confirmation Dialog" );
    alert.setContentText( "Are you ok with this?" );

    System.out.println( "timeline Started -" + Calendar.getInstance().getTime() );
    Timeline idlestage = new Timeline( new KeyFrame( Duration.seconds(5 ), new EventHandler<ActionEvent>()
    {

        @Override
        public void handle( ActionEvent event )
        {
            alert.setResult(ButtonType.CANCEL);
            alert.hide();
        }
    } ) );
    idlestage.setCycleCount( 1 );
    idlestage.play();

    Optional<ButtonType> result = alert.showAndWait();

    if ( result.get() == ButtonType.OK )
    {
        System.out.println( "ok clicked" );
    }
    else if ( result.get() == ButtonType.CANCEL)
    {
        System.out.println( "cancel clicked" );
    }

    stage.setScene( scene );
    stage.show();
}

关于java - 超时后自动关闭对话框并取消响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391905/

相关文章:

c - ATMega 随机数生成

具有定时器控制的线程池操作的 C# WinService

c# - Java 中 C# 中的 Timer 等价物?

JavaFX Spring Boot @Autowire 服务为空

JavaFx:使用webengine在服务器程序中获取javascript渲染的dom

java - 如何使用 actionbarsherlock 创建自定义下拉菜单?

java - 在 Java 中定义一个 static final 变量有意义吗?

Java GSON 自定义反序列化器版本控制支持

java - JavaFx :The image doesn't show 上的问题

java - 通过 Spring XmlWebApplicationContext 指定相对资源路径