From 66186a342d64123c3e347d386fbd3cfe77c21db2 Mon Sep 17 00:00:00 2001 From: lantzelot-swe <75668734+lantzelot-swe@users.noreply.github.com> Date: Sat, 5 Mar 2022 15:18:12 +0100 Subject: [PATCH] fix: focus is set to the URL field in the scraper dialog and the URL is selected when opened --- .../lantz/gui/scraper/MobyGamesOptionsPanel.java | 15 +++++++++++++++ .../java/se/lantz/gui/scraper/ScraperDialog.java | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/se/lantz/gui/scraper/MobyGamesOptionsPanel.java b/src/main/java/se/lantz/gui/scraper/MobyGamesOptionsPanel.java index 02c5f19..dfef671 100644 --- a/src/main/java/se/lantz/gui/scraper/MobyGamesOptionsPanel.java +++ b/src/main/java/se/lantz/gui/scraper/MobyGamesOptionsPanel.java @@ -8,6 +8,8 @@ import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.IOException; @@ -136,6 +138,19 @@ public class MobyGamesOptionsPanel extends JPanel if (urlTextField == null) { urlTextField = new JTextField(); + urlTextField.addFocusListener(new FocusListener() { + + @Override + public void focusGained(FocusEvent e) { + urlTextField.select(0, urlTextField.getText().length()); + } + + @Override + public void focusLost(FocusEvent e) { + urlTextField.select(0, 0); + } + }); + urlTextField.setText("https://www.mobygames.com/game/"); urlTextField.addKeyListener(new KeyAdapter() { diff --git a/src/main/java/se/lantz/gui/scraper/ScraperDialog.java b/src/main/java/se/lantz/gui/scraper/ScraperDialog.java index 469369e..1111c29 100644 --- a/src/main/java/se/lantz/gui/scraper/ScraperDialog.java +++ b/src/main/java/se/lantz/gui/scraper/ScraperDialog.java @@ -12,6 +12,7 @@ import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingUtilities; import se.lantz.gui.BaseDialog; import se.lantz.manager.ScraperManager; @@ -106,7 +107,7 @@ public class ScraperDialog extends BaseDialog getMobyGamesPanel().preSelectFields(); getC64comPanel().preSelectFields(); getGb64comPanel().preSelectFields(); - getMobyGamesPanel().getUrlTextField().requestFocusInWindow(); + SwingUtilities.invokeLater(() -> getMobyGamesPanel().getUrlTextField().requestFocusInWindow()); this.getRootPane().setDefaultButton(getMobyGamesPanel().getConnectButton()); return super.showDialog();