|
31 | 31 |
|
32 | 32 | import javax.swing.Icon; |
33 | 33 | import javax.swing.JOptionPane; |
34 | | -import java.awt.Component; |
| 34 | +import java.awt.*; |
| 35 | +import java.lang.reflect.InvocationTargetException; |
35 | 36 | import java.util.ArrayList; |
36 | 37 | import java.util.List; |
| 38 | +import java.util.concurrent.CompletableFuture; |
| 39 | +import java.util.concurrent.ExecutionException; |
37 | 40 |
|
38 | 41 | /** |
39 | 42 | * Utility class offering dialog-box-related methods. |
@@ -64,14 +67,24 @@ public static Result ask(Component parent, |
64 | 67 | "At least one of yes, no, or never must be non-null"); |
65 | 68 | } |
66 | 69 | 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) { |
70 | 83 | case 0: return Result.YES; |
71 | 84 | case 1: return Result.NO; |
72 | 85 | case 2: return Result.NEVER; |
73 | 86 | case -1: return Result.CANCELED; |
74 | | - default: throw new RuntimeException("Unexpected value: " + rval); |
| 87 | + default: throw new RuntimeException("Unexpected value: " + choice); |
75 | 88 | } |
76 | 89 | } |
77 | 90 |
|
|
0 commit comments