From ff3a5f34af2add395b99e88f87ff2f6a10b86c07 Mon Sep 17 00:00:00 2001 From: Jen Warman Date: Sat, 2 Mar 2024 10:29:05 +0000 Subject: [PATCH 1/2] fix: Add focus to first field in macro popup --- .../universalgcodesender/MacroHelper.java | 3 +++ .../RequestFocusListener.java | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java diff --git a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java index 20f8ca9ae3..c020a4d970 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java @@ -166,6 +166,9 @@ private static String parsePrompts(String command) { myPanel.setLayout(new MigLayout("wrap 2, width 200")); for (Prompt s : prompts) { JTextField field = new JTextField(s.defaultValue); + if (fields.size() == 0){ + field.addHierarchyListener(new RequestFocusListener()); + } myPanel.add(new JLabel(s.prompt + ":")); myPanel.add(field, "growx, pushx"); fields.add(field); diff --git a/ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java b/ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java new file mode 100644 index 0000000000..980afcc5c4 --- /dev/null +++ b/ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java @@ -0,0 +1,27 @@ +package com.willwinder.universalgcodesender; + +import java.awt.Component; +import java.awt.Window; +import java.awt.event.HierarchyEvent; +import java.awt.event.HierarchyListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import javax.swing.SwingUtilities; + +public class RequestFocusListener implements HierarchyListener { + + @Override + public void hierarchyChanged(HierarchyEvent e) { + final Component c = e.getComponent(); + if (c.isShowing() && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { + Window toplevel = SwingUtilities.getWindowAncestor(c); + toplevel.addWindowFocusListener(new WindowAdapter() { + + @Override + public void windowGainedFocus(WindowEvent e) { + c.requestFocus(); + } + }); + } + } +} \ No newline at end of file From b5a2818392255734832a41d311f5fbfd2af2b6d9 Mon Sep 17 00:00:00 2001 From: Jen Warman Date: Sun, 3 Mar 2024 17:32:03 +0000 Subject: [PATCH 2/2] Cleanup --- .../com/willwinder/universalgcodesender/MacroHelper.java | 1 + .../{ => uielements/components}/RequestFocusListener.java | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) rename ugs-core/src/com/willwinder/universalgcodesender/{ => uielements/components}/RequestFocusListener.java (80%) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java index c020a4d970..bcc6094fe9 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java @@ -22,6 +22,7 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.BackendAPI; import com.willwinder.universalgcodesender.model.Position; import com.willwinder.universalgcodesender.services.KeyboardService; +import com.willwinder.universalgcodesender.uielements.components.RequestFocusListener; import net.miginfocom.swing.MigLayout; import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; diff --git a/ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java b/ugs-core/src/com/willwinder/universalgcodesender/uielements/components/RequestFocusListener.java similarity index 80% rename from ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java rename to ugs-core/src/com/willwinder/universalgcodesender/uielements/components/RequestFocusListener.java index 980afcc5c4..741b4ba1ad 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/RequestFocusListener.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/uielements/components/RequestFocusListener.java @@ -1,4 +1,4 @@ -package com.willwinder.universalgcodesender; +package com.willwinder.universalgcodesender.uielements.components; import java.awt.Component; import java.awt.Window; @@ -8,6 +8,11 @@ import java.awt.event.WindowEvent; import javax.swing.SwingUtilities; +/** + * Used for focusing a field in an JOptionDialog. + * + * Source from: https://tips4java.wordpress.com/2010/03/14/dialog-focus/ + */ public class RequestFocusListener implements HierarchyListener { @Override