Skip to content

Commit

Permalink
fix: Add focus to first field in macro popup
Browse files Browse the repository at this point in the history
  • Loading branch information
JenWarman committed Mar 2, 2024
1 parent 2dc4613 commit b5badb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ private static String parsePrompts(String command) {
myPanel.setLayout(new MigLayout("wrap 2, width 200"));
for (String s : prompts) {
JTextField field = new JTextField();
if (fields.size() == 0){
field.addHierarchyListener(new RequestFocusListener());
}
myPanel.add(new JLabel(s + ":"));
myPanel.add(field, "growx, pushx");
fields.add(field);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
});
}
}
}

0 comments on commit b5badb5

Please sign in to comment.