<p>For most GUI's I've used, when a control that contains text gets the focus, the entire contents of the control are selected.  This means if you just start typing, you completely replace the former contents.</p>

<p>Example:  You have spin control that is initialized with the value zero.   You tab to it and type "1" The value in the control is now 1.</p>

<p>With Swing, this doesn't happen.  The text in the control is not selected and the carat appears at one end or another of the existing text.  Continuing the above example:</p>

<p>With a Swing JSpinner, when you tab to the spin control, the carat is at the left.  You type "1" and the value in the control is now 10.</p>

<p>This drives me, (and my users) up a wall, and I'd like to change it.  Even more important, I'd like to change it globally so the new behavior applies to JTextField, JPasswordField, JFormattedTextField, JTextArea, JComboBox, JSpinner, and so on.   The only way I have found to do this to add a FocusAdapter to each control and override the focusGained() method to Do The Right Thing[tm].</p>

<p>There's gotta be an easier, and less fragile way.  Please?</p>

<p>EDIT: One additional piece of information for this particular case.  The form I am working with was generated using Idea's form designer.  That means I normally don't actually write the code to create the components.  It is possible to tell Idea that you want to create them yourself, but that's a hassle I'd like to avoid.</p>

<p>Motto:  All good programmers are basically lazy.</p>

[{'answer_id': 66570, 'author': 'Alexandre Brasil', 'author_id': 8841, 'author_profile': 'https://Stackoverflow.com/users/8841', 'pm_score': 0, 'selected': False, 'text': '<p>The only way I know is to create a FocusListener and attach it to your component. If you want it this FocusListener to be global to all components in your application you might consider using Aspect Oriented Programming (AOP). With AOP is possible to code it once and apply your focus listener to all components instantiated in your app without having to copy-and-paste the <em>component.addFocusListener(listener)</em> code throughout your application..</p>\n\n<p>Your aspect would have to intercept the creation of a JComponent (or the sub-classes you want to add this behaviour to) and add the focus listener to the newly created instance. The AOP approach is better than copy-and-pasting the FocusListener to your entire code because you keep it all in a single piece of code, and don\'t create a maintenance nightmare once you decide to change your global behavior like removing the listener for JSpinners.</p>\n\n<p>There are many AOP frameworks out there to choose from. I like <a href="http://www.jboss.org/jbossaop/" rel="nofollow noreferrer">JBossAOP</a> since it\'s 100% pure Java, but you might like to take a look at <a href="http://www.eclipse.org/aspectj/" rel="nofollow noreferrer">AspectJ</a>.</p>\n'}, {'answer_id': 66645, 'author': 'rcreswick', 'author_id': 3446, 'author_profile': 'https://Stackoverflow.com/users/3446', 'pm_score': 2, 'selected': False, 'text': '<p>When I\'ve needed this in the past, I\'ve created subclasses of the components I wanted to add "auto-clearing" functionality too.  eg:</p>\n\n<pre><code>public class AutoClearingTextField extends JTextField {\n   final FocusListener AUTO_CLEARING_LISTENER = new FocusListener(){\n      @Override\n      public void focusLost(FocusEvent e) {\n         //onFocusLost(e);\n      }\n\n      @Override\n      public void focusGained(FocusEvent e) {\n         selectAll();\n      }\n   };\n\n   public AutoClearingTextField(String string) {\n      super(string);\n      addListener();\n   }\n\n   private void addListener() {\n      addFocusListener(AUTO_CLEARING_LISTENER);      \n   }\n}\n</code></pre>\n\n<p>The biggest problem is that I haven\'t found a "good" way to get all the standard constructors without writing overrides.  Adding them, and forcing a call to addListener is the most general approach I\'ve found.</p>\n\n<p>Another option is to watch for ContainerEvents on a top-level container with a ContainerListeer to detect the presence of new widgets, and add a corresponding focus listener based on the widgets that have been added. (eg: if the container event is caused by adding a TextField, then add a focus listener that knows how to select all the text in a TextField, and so on.)  If a Container is added, then you need to recursively add the ContainerListener to that new sub-container as well.</p>\n\n<p>Either way, you won\'t need to muck about with focus listeners in your actual UI code -- it will all be taken care of at a higher level.</p>\n'}, {'answer_id': 67376, 'author': 'Avrom', 'author_id': 8840, 'author_profile': 'https://Stackoverflow.com/users/8840', 'pm_score': 2, 'selected': False, 'text': "<p>I haven't tried this myself (only dabbled in it a while ago), but you can probably get the current focused component by using:\nKeyboardFocusManager (there is a static method getCurrentKeyboardFocusManager())\nan adding a PropertyChangeListener to it.\nFrom there, you can find out if the component is a JTextComponent and select all text.</p>\n"}, {'answer_id': 75244, 'author': 'shemnon', 'author_id': 8020, 'author_profile': 'https://Stackoverflow.com/users/8020', 'pm_score': 2, 'selected': False, 'text': '<p>A separate class that attaches a FocusListener to the desired text field can be written.  All the focus listener would do is call selectAll() on the text widget when it gains the focus.</p>\n\n<pre><code>public class SelectAllListener implements FocusListener {\n  private static INSTANCE = new SelectAllListener();\n\n  public void focusLost(FocusEvent e) { }\n\n  public void focusGained(FocusEvent e) {\n    if (e.getSource() instanceof JTextComponent) {  \n      ((JTextComponent)e.getSource()).selectAll();\n    }\n  };\n\n  public static void addSelectAllListener(JTextComponent tc) {\n    tc.addFocusListener(INSTANCE);\n  }\n\n  public static void removeSelectAllListener(JTextComponent tc) {\n    tc.removeFocusListener(INSTANCE);\n  }\n}\n</code></pre>\n\n<p>By accepting a JTextComponent as an argument this behavior can be added to JTextArea, JPasswordField, and all of the other text editing components directly.  This also allows the class to add select all to editable combo boxes and JSpinners, where your control over the text editor component may be more limited.  Convenience methods can be added:</p>\n\n<pre><code>public static void addSelectAllListener(JSpinner spin) {\n  if (spin.getEditor() instanceof JTextComponent) {\n    addSelectAllListener((JTextComponent)spin.getEditor());\n  }\n}\n\npublic static void addSelectAllListener(JComboBox combo) {\n  JComponent editor = combo.getEditor().getEditorComponent();\n  if (editor instanceof JTextComponent) {\n    addSelectAllListener((JTextComponent)editor);\n  }\n}\n</code></pre>\n\n<p>Also, the remove listener methods are likely unneeded, since the listener contains no exterior references to any other instances, but they can be added to make code reviews go smoother.</p>\n'}, {'answer_id': 86170, 'author': 'Dale Wilson', 'author_id': 391806, 'author_profile': 'https://Stackoverflow.com/users/391806', 'pm_score': 2, 'selected': True, 'text': "<p>After reading the replies so far (Thanks!) I passed the outermost JPanel to the following method: </p>\n\n<pre><code>void addTextFocusSelect(JComponent component){\n    if(component instanceof JTextComponent){\n        component.addFocusListener(new FocusAdapter() {\n                @Override\n                public void focusGained(FocusEvent event) {\n                    super.focusGained(event);\n                    JTextComponent component = (JTextComponent)event.getComponent();\n                    // a trick I found on JavaRanch.com\n                    // Without this, some components don't honor selectAll\n                    component.setText(component.getText());\n                    component.selectAll();\n                }\n            });\n\n    }\n    else\n    {\n        for(Component child: component.getComponents()){\n            if(child instanceof JComponent){\n                addTextFocusSelect((JComponent) child);\n            }\n        }\n    }\n}\n</code></pre>\n\n<p>It works!</p>\n"}]
['https://Stackoverflow.com/questions/66455', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/391806/']