diff --git a/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java b/ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java index 20f8ca9ae3..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; @@ -166,6 +167,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/uielements/components/RequestFocusListener.java b/ugs-core/src/com/willwinder/universalgcodesender/uielements/components/RequestFocusListener.java new file mode 100644 index 0000000000..741b4ba1ad --- /dev/null +++ b/ugs-core/src/com/willwinder/universalgcodesender/uielements/components/RequestFocusListener.java @@ -0,0 +1,32 @@ +package com.willwinder.universalgcodesender.uielements.components; + +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; + +/** + * 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 + 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