From c9b65df2568928c9c8fad4c9bc276796e6604f95 Mon Sep 17 00:00:00 2001 From: hneemann Date: Sat, 27 May 2017 23:10:32 +0200 Subject: [PATCH] Top most string field gets focus if the attribute dialog is opened --- .../de/neemann/digital/gui/components/AttributeDialog.java | 7 +++++++ .../de/neemann/digital/gui/components/EditorFactory.java | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/neemann/digital/gui/components/AttributeDialog.java b/src/main/java/de/neemann/digital/gui/components/AttributeDialog.java index 83781fea7..f95610702 100644 --- a/src/main/java/de/neemann/digital/gui/components/AttributeDialog.java +++ b/src/main/java/de/neemann/digital/gui/components/AttributeDialog.java @@ -31,6 +31,7 @@ public class AttributeDialog extends JDialog { private final Point pos; private final ElementAttributes elementAttributes; private final JPanel buttonPanel; + private JComponent topMostTextComponent; private VisualElement visualElement; private boolean changed = false; @@ -66,8 +67,11 @@ public class AttributeDialog extends JDialog { editors = new ArrayList<>(); + topMostTextComponent = null; for (Key key : list) { Editor e = EditorFactory.INSTANCE.create(key, elementAttributes.get(key)); + if (topMostTextComponent == null && e instanceof EditorFactory.StringEditor) + topMostTextComponent = ((EditorFactory.StringEditor) e).getTextComponent(); editors.add(new EditorHolder(e, key)); e.addToPanel(panel, key, elementAttributes, this); } @@ -154,6 +158,9 @@ public class AttributeDialog extends JDialog { else setLocation(pos.x, pos.y); + if (topMostTextComponent != null) + SwingUtilities.invokeLater(() -> topMostTextComponent.requestFocusInWindow()); + setVisible(true); return changed; } diff --git a/src/main/java/de/neemann/digital/gui/components/EditorFactory.java b/src/main/java/de/neemann/digital/gui/components/EditorFactory.java index 6bedfc126..c142b6681 100644 --- a/src/main/java/de/neemann/digital/gui/components/EditorFactory.java +++ b/src/main/java/de/neemann/digital/gui/components/EditorFactory.java @@ -128,7 +128,7 @@ public final class EditorFactory { //Checkstyle flags redundant modifiers, which are not redundant. Maybe a bug in checkstyle? //CHECKSTYLE.OFF: RedundantModifier - private final static class StringEditor extends LabelEditor { + final static class StringEditor extends LabelEditor { private final JTextComponent text; private final JComponent compToAdd; @@ -155,6 +155,9 @@ public final class EditorFactory { return text.getText().trim(); } + public JTextComponent getTextComponent() { + return text; + } } private final static class IntegerEditor extends LabelEditor {