Skip to content

Commit 7e6a107

Browse files
committed
Avoid null parents when displaying dialogs
According to ChatGPT: Sometimes, Java UI components require elevated permissions to manage window focus or modal dialogs. When running with lower privileges, JOptionPane may be blocked by another process or hidden behind other windows, causing an apparent "freeze." Explicitly creating a parent frame (even if it's not "shown") appears to be enough to get around these privilige problems. Closes apposed/jaunch#71
1 parent 519b2f6 commit 7e6a107

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929

3030
package org.scijava.launcher;
3131

32-
import javax.swing.Icon;
33-
import javax.swing.JOptionPane;
32+
import javax.swing.*;
3433
import java.awt.*;
3534
import java.lang.reflect.InvocationTargetException;
35+
import java.security.AccessController;
36+
import java.security.PrivilegedAction;
3637
import java.util.ArrayList;
3738
import java.util.List;
3839
import java.util.concurrent.CompletableFuture;
@@ -67,9 +68,21 @@ public static Result ask(Component parent,
6768
"At least one of yes, no, or never must be non-null");
6869
}
6970
Object initial = no == null ? options[0] : no;
71+
7072
CompletableFuture<Integer> future = new CompletableFuture<>();
71-
EventQueue.invokeLater(() -> {
72-
int result =JOptionPane.showOptionDialog(parent, message, title, optionType, messageType, icon, options, initial);
73+
SwingUtilities.invokeLater(() -> {
74+
Component parentComp = parent;
75+
boolean disposeParent = false;
76+
if (parentComp == null) {
77+
parentComp = new JFrame();
78+
((JFrame)parentComp).setAlwaysOnTop(true);
79+
((JFrame)parentComp).setLocationRelativeTo(null); // Ensure it's centered
80+
disposeParent = true;
81+
}
82+
int result = JOptionPane.showOptionDialog(parentComp, message, title, optionType, messageType, icon, options, initial);
83+
if (disposeParent) {
84+
((JFrame) parentComp).dispose();
85+
}
7386
future.complete(result);
7487
});
7588
int choice = 0; // Blocks until dialog completes

0 commit comments

Comments
 (0)