我有一个JavaFx应用程序,在其中我使用以下方式显示警告框:
alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
buttonTypeOne = new ButtonType("Yes");
buttonTypeCancel = new ButtonType("No", ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Optional<ButtonType> result = alert.showAndWait();
因为警报会执行showAndWait
,所以它会显示,直到用户按下“是”&#39;或者&#39;否&#39;或者&#39;关闭&#39;对话框。
问题:我需要从其他地方以编程方式关闭此对话框。详细说明,如果,让我们说用户没有选择任何选项20秒,或者让我们说这个警告框已经显示了刚刚结束的一些后台进程,现在我想关闭这个将result
设置为buttonTypeCancel
,而不是用户按任意按钮,即可显示提示框。
(与Swing中的dispose
方法类似)
我该怎么做?我尝试了Event.fireevent(https://stackoverflow.com/a/22459308/3155986),但我无法编写正确的事件。
提前致谢!
编辑:包括示例代码 -
Compute.java - 另一个执行计算的java类。
public class Compute { 警报提醒;
public void function1{
Platform.runLater(new Runnable(){
public void run(){
alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
buttonTypeOne = new ButtonType("Yes");
buttonTypeCancel = new ButtonType("No", ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
// ... user chose "One"
} else {
// ... user chose CANCEL or closed the dialog
}
}
});
}
public void function2{
//......Does some long computation here
//.... If the computation finishes before the user chooses 'Yes' or 'No' on alert box
//...Then close the alertbox here and execute the code corresponding to buttonTypeCancel
//..I tried alert.close(); and alert.hide(); but doesn't work.
}
}
此外,还有其他替代解决方案吗?最初,我想保持Compute.java
清除任何javafx代码,但无法弄清楚如何。
答案 0 :(得分:2)
您可以在警报或任何对话框上调用close()
方法。
这是一个等待5秒的简单示例,如果警报仍在显示,则会关闭它。
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import java.util.Optional;
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
ButtonType buttonTypeOne = new ButtonType("Yes");
ButtonType buttonTypeCancel = new ButtonType("No", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Thread thread = new Thread(() -> {
try {
// Wait for 5 secs
Thread.sleep(5000);
if (alert.isShowing()) {
Platform.runLater(() -> alert.close());
}
} catch (Exception exp) {
exp.printStackTrace();
}
});
thread.setDaemon(true);
thread.start();
Optional<ButtonType> result = alert.showAndWait();
}
}
答案 1 :(得分:2)
试试这个
public void function2 {
Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( buttonTypeCancel );
cancelButton.fire();
}
或者更一般的
public void function2 {
for ( ButtonType bt : alert.getDialogPane().getButtonTypes() )
{
if ( bt.getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE )
{
Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( bt );
cancelButton.fire();
break;
}
}
}
完整示例:
@Override
public void start( final Stage primaryStage )
{
Alert alert = new Alert( Alert.AlertType.CONFIRMATION );
alert.setTitle( "Title" );
alert.setHeaderText( "Some Text" );
alert.setContentText( "Choose your option." );
ButtonType buttonTypeOne = new ButtonType( "Yes" );
alert.initModality( Modality.NONE );
ButtonType buttonTypeCancel = new ButtonType( "No", ButtonBar.ButtonData.CANCEL_CLOSE );
alert.getButtonTypes().setAll( buttonTypeOne, buttonTypeCancel );
Button b = new Button( "close alert" );
b.setOnAction(( ActionEvent event ) ->
{
for ( ButtonType bt : alert.getDialogPane().getButtonTypes() )
{
System.out.println( "bt = " + bt );
if ( bt.getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE )
{
Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( bt );
cancelButton.fire();
break;
}
}
});
final Scene scene = new Scene( new Group( b ), 400, 300 );
primaryStage.setScene( scene );
primaryStage.show();
Optional<ButtonType> result = alert.showAndWait();
if ( result.get() == buttonTypeOne )
{
System.out.println( "one " );
}
else if( result.get() == buttonTypeCancel )
{
System.out.println( "cancel " );
}
}
答案 2 :(得分:1)
它在2秒内关闭了对话框。
dialog = new Dialog<>();
dialog.setTitle("It's a dialog!");
dialog.show();
Thread newThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
Platform.runLater(new Runnable() {
@Override
public void run() {
dialog.close();
}
});
}
});
newThread.start();