Skip to content

Commit

Permalink
Show dialog on EDT
Browse files Browse the repository at this point in the history
  • Loading branch information
hinerm committed Feb 6, 2025
1 parent 36b10dd commit 519b2f6
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/main/java/org/scijava/launcher/Dialogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@

import javax.swing.Icon;
import javax.swing.JOptionPane;
import java.awt.Component;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
* Utility class offering dialog-box-related methods.
Expand Down Expand Up @@ -64,14 +67,24 @@ public static Result ask(Component parent,
"At least one of yes, no, or never must be non-null");
}
Object initial = no == null ? options[0] : no;
int rval = JOptionPane.showOptionDialog(parent, message,
title, optionType, messageType, icon, options, initial);
switch (rval) {
CompletableFuture<Integer> future = new CompletableFuture<>();
EventQueue.invokeLater(() -> {
int result =JOptionPane.showOptionDialog(parent, message, title, optionType, messageType, icon, options, initial);
future.complete(result);
});
int choice = 0; // Blocks until dialog completes
try {
choice = future.get();
}
catch (InterruptedException | ExecutionException e) {
choice = -1;
}
switch (choice) {
case 0: return Result.YES;
case 1: return Result.NO;
case 2: return Result.NEVER;
case -1: return Result.CANCELED;
default: throw new RuntimeException("Unexpected value: " + rval);
default: throw new RuntimeException("Unexpected value: " + choice);
}
}

Expand Down

0 comments on commit 519b2f6

Please sign in to comment.