自动关闭对话框,超时后取消响应

时间:2015-09-04 06:57:42

标签: 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秒后隐藏,但无法获得任何Dialog响应。

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

1 个答案:

答案 0 :(得分:1)

直接设置结果正常。

@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();
}