Gridarta Editor
TextComponentUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.utils;
21 
22 import java.awt.Component;
23 import java.awt.event.ActionListener;
24 import java.awt.event.FocusEvent;
25 import java.awt.event.FocusListener;
26 import javax.swing.JTextField;
27 import javax.swing.text.JTextComponent;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class TextComponentUtils {
35 
39  private TextComponentUtils() {
40  }
41 
47  public static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent) {
48  final FocusListener focusListener = new FocusListener() {
49 
50  @Override
51  public void focusGained(@NotNull final FocusEvent e) {
52  textComponent.selectAll();
53  }
54 
55  @Override
56  public void focusLost(@NotNull final FocusEvent e) {
57  // ignore
58  }
59 
60  };
61  textComponent.addFocusListener(focusListener);
62  }
63 
69  public static void setActionNextFocus(@NotNull final JTextField textField, @NotNull final Component nextComponent) {
70  final ActionListener actionListener = e -> nextComponent.requestFocusInWindow();
71  textField.addActionListener(actionListener);
72  }
73 
74 }
net.sf.gridarta.gui.utils.TextComponentUtils.TextComponentUtils
TextComponentUtils()
Private constructor to prevent instantiation.
Definition: TextComponentUtils.java:39
net.sf.gridarta.gui.utils.TextComponentUtils.setActionNextFocus
static void setActionNextFocus(@NotNull final JTextField textField, @NotNull final Component nextComponent)
Transfers the focus to another component when ENTER is pressed.
Definition: TextComponentUtils.java:69
net.sf.gridarta.gui.utils.TextComponentUtils.setAutoSelectOnFocus
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
Definition: TextComponentUtils.java:47
net.sf.gridarta.gui.utils.TextComponentUtils
Utility class for JTextComponent related functions.
Definition: TextComponentUtils.java:34