线程任务完成后,JavaFX显示对话

时间:2015-09-25 00:47:52

标签: java multithreading javafx

我需要显示对话窗口

<html>
<body>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600' rel='stylesheet' type='text/css'>
<link href="//netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.css" rel="stylesheet">
<?php echo $output; ?>

<div class="testbox">
	<h1>Registration</h1>
		<form action="assets/php/mail.php" method="post">
<hr>
<div class="accounttype">
	<input type="radio" value="student" id="student" name="accounttype" checked/>
	<label for="student" class="radio" chec>Student</label>
	<input type="radio" value="faculty" id="faculty" name="accounttype" />
	<label for="faculty" class="radio">Faculty</label>
</div>
<hr>
	<label id="icon" for="name"><i class="icon-user "></i></label>
	<input type="text" name="firstname" id="firstname" placeholder="First Name" required/>
	<label id="icon" for="name"><i class="icon-user"></i></label>
	<input type="text" name="lastname" id="lastname" placeholder="Last Name" required/>
	<label id="icon" for="name"><i class="icon-shield"></i></label>
	<input type="text" name="middlename" id="middlename" placeholder="Middle Name"/>
<hr>
	<label id="icon" for="name"><i class="icon-envelope "></i></label>
	<input type="email" name="email" id="email" placeholder="Email Address" required/>
	<label id="icon" for="name"><i class="icon-map-marker "></i></label>
	<input type="text" name="location" id="location" placeholder="City, State" required/>
	<label id="icon" for="name"><i class="icon-shield"></i></label>
	<input type="number" name="year" id="year" placeholder="Graduation Year" required/>
<hr>
<div class="gender">
	<input type="radio" value="male" id="male" name="gender" checked/>
	<label for="male" class="radio" chec>Male</label>
	<input type="radio" value="female" id="female" name="gender" />
	<label for="female" class="radio">Female</label>
<hr>
	<p>Upload full body image</p>
    <input type="file" accept="image/*;capture=camera">
</div> 
<p>By clicking Register, you agree on our <a href="#">terms and condition</a>.</p>
	<input type="submit" class="button" value="Send" />
</form>
</div>
</body>
</html>

我的线程完成任务后

 Stage dialog = new Stage();
            dialog.initStyle(StageStyle.UTILITY);
            Scene scene = new Scene(new Group(new Text(25, 25, "All is done!")));
            dialog.setScene(scene);
            dialog.showAndWait();   

我已经尝试了

Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                   doSomeStuff();
                }

            });

但是这个应用在Thread t = new Thread(new Runnable() { @Override public void run() { doSomeStuff(); } }); t.start(); t.join(); Stage dialog = new Stage(); dialog.initStyle(StageStyle.UTILITY); Scene scene = new Scene(new Group(new Text(25, 25, "All is done!"))); dialog.setScene(scene); dialog.showAndWait(); } 完成之前没有响应

1 个答案:

答案 0 :(得分:11)

t.join()是一个阻塞调用,因此它将阻止FX Application线程,直到后台线程完成。这将阻止重新绘制UI或响应用户输入。

最简单的方法是使用Task

Task<Void> task = new Task<Void>() {
    @Override
    public Void call() throws Exception {
        doSomeStuff();
        return null ;
    }
};
task.setOnSucceeded(e -> {
    Stage dialog = new Stage();
    dialog.initStyle(StageStyle.UTILITY);
    Scene scene = new Scene(new Group(new Text(25, 25, "All is done!")));
    dialog.setScene(scene);
    dialog.showAndWait();
});
new Thread(task).start();

低级别(即不使用高级API JavaFX提供)方法是从后台线程安排在FX Application线程上显示对话框:

Thread t = new Thread(() -> {
    doSomeStuff();
    Platform.runLater(() -> {
        Stage dialog = new Stage();
        dialog.initStyle(StageStyle.UTILITY);
        Scene scene = new Scene(new Group(new Text(25, 25, "All is done!")));
        dialog.setScene(scene);
        dialog.showAndWait();
    });
});
t.start();

我强烈建议使用第一种方法。