removes focus moving, see #756

This commit is contained in:
hneemann 2021-05-29 18:29:16 +02:00
parent 467064ffa1
commit 674ba18adb
4 changed files with 20 additions and 8 deletions

View File

@ -35,7 +35,7 @@ public class GraphicDialog extends JDialog {
setLocationRelativeTo(null); setLocationRelativeTo(null);
setVisible(true); setVisible(true);
addWindowFocusListener(new MoveFocusTo(parent)); MoveFocusTo.addListener(this, parent);
} }
/** /**

View File

@ -37,7 +37,7 @@ public class LedMatrixDialog extends JDialog {
setLocationRelativeTo(null); setLocationRelativeTo(null);
setVisible(true); setVisible(true);
addWindowFocusListener(new MoveFocusTo(parent)); MoveFocusTo.addListener(this, parent);
} }
/** /**

View File

@ -19,21 +19,33 @@ import java.awt.event.WindowFocusListener;
* no longer be used. * no longer be used.
*/ */
public class MoveFocusTo implements WindowFocusListener { public class MoveFocusTo implements WindowFocusListener {
private final Window parent;
/** /**
* Creates a new instance * Adds the listener to the given child dialog and moves the focus to the given parent
* *
* @param parent the window which should keep the focus * @param child the child dialog
* @param parent the parent window
*/ */
public MoveFocusTo(Window parent) { public static void addListener(Dialog child, Window parent) {
child.setAlwaysOnTop(true);
child.addWindowFocusListener(new MoveFocusTo(child, parent));
}
private final Window child;
private final Window parent;
private MoveFocusTo(Window child, Window parent) {
this.child = child;
this.parent = parent; this.parent = parent;
} }
@Override @Override
public void windowGainedFocus(WindowEvent windowEvent) { public void windowGainedFocus(WindowEvent windowEvent) {
if (parent != null) if (parent != null)
SwingUtilities.invokeLater(parent::requestFocus); SwingUtilities.invokeLater(() -> {
child.removeWindowFocusListener(this);
parent.requestFocus();
});
} }
@Override @Override

View File

@ -33,7 +33,7 @@ public class VGADialog extends JDialog {
setLocationRelativeTo(null); setLocationRelativeTo(null);
setVisible(true); setVisible(true);
addWindowFocusListener(new MoveFocusTo(parent)); MoveFocusTo.addListener(this, parent);
} }
/** /**