Skip to content

Commit 519b2f6

Browse files
committed
Show dialog on EDT
1 parent 36b10dd commit 519b2f6

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/main/java/org/scijava/launcher/Dialogs.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131

3232
import javax.swing.Icon;
3333
import javax.swing.JOptionPane;
34-
import java.awt.Component;
34+
import java.awt.*;
35+
import java.lang.reflect.InvocationTargetException;
3536
import java.util.ArrayList;
3637
import java.util.List;
38+
import java.util.concurrent.CompletableFuture;
39+
import java.util.concurrent.ExecutionException;
3740

3841
/**
3942
* Utility class offering dialog-box-related methods.
@@ -64,14 +67,24 @@ public static Result ask(Component parent,
6467
"At least one of yes, no, or never must be non-null");
6568
}
6669
Object initial = no == null ? options[0] : no;
67-
int rval = JOptionPane.showOptionDialog(parent, message,
68-
title, optionType, messageType, icon, options, initial);
69-
switch (rval) {
70+
CompletableFuture<Integer> future = new CompletableFuture<>();
71+
EventQueue.invokeLater(() -> {
72+
int result =JOptionPane.showOptionDialog(parent, message, title, optionType, messageType, icon, options, initial);
73+
future.complete(result);
74+
});
75+
int choice = 0; // Blocks until dialog completes
76+
try {
77+
choice = future.get();
78+
}
79+
catch (InterruptedException | ExecutionException e) {
80+
choice = -1;
81+
}
82+
switch (choice) {
7083
case 0: return Result.YES;
7184
case 1: return Result.NO;
7285
case 2: return Result.NEVER;
7386
case -1: return Result.CANCELED;
74-
default: throw new RuntimeException("Unexpected value: " + rval);
87+
default: throw new RuntimeException("Unexpected value: " + choice);
7588
}
7689
}
7790

0 commit comments

Comments
 (0)