Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7d29c0

Browse files
committed
SwingDialog: ignore ENTER for non-modal dialogs
See: https://forum.image.sc/t/command-ui-closes-after-pressing-enter-key/19510
1 parent bb73f4c commit d7d29c0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/main/java/org/scijava/ui/swing/SwingDialog.java

+13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.awt.BorderLayout;
3434
import java.awt.Component;
35+
import java.awt.Container;
3536
import java.awt.Insets;
3637
import java.awt.Window;
3738
import java.awt.event.WindowAdapter;
@@ -203,6 +204,9 @@ public boolean isModal() {
203204
public void setModal(final boolean modal) {
204205
this.modal = modal;
205206
buttons.setVisible(modal);
207+
208+
// disable the buttons if non-modal, to avoid ENTER closing the dialog.
209+
setEnabled(buttons, modal);
206210
}
207211

208212
/** Gets whether the dialog is resizable by the user. */
@@ -351,4 +355,13 @@ public void windowGainedFocus(final WindowEvent e) {
351355
});
352356
}
353357

358+
/** Recursively enable or disable components. */
359+
private void setEnabled(final Component c, final boolean enabled) {
360+
if (c instanceof Container) {
361+
for (final Component child : ((Container) c).getComponents()) {
362+
setEnabled(child, enabled);
363+
}
364+
}
365+
c.setEnabled(enabled);
366+
}
354367
}

0 commit comments

Comments
 (0)