-
-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set focus to first field in macro popup (#2474)
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...e/src/com/willwinder/universalgcodesender/uielements/components/RequestFocusListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); | ||
} | ||
} | ||
} |