Skip to content

Instantly share code, notes, and snippets.

@seanh
Created May 25, 2009 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanh/117582 to your computer and use it in GitHub Desktop.
Save seanh/117582 to your computer and use it in GitHub Desktop.
A JTextField subclass that automatically selects all text when focused for the first time.
package storymaps;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A JTextField that automatically selects all text when focused IF the text
* it currently contains is the initial text that it was constructed with.
* The text field also deselects the text when it loses focus.
*
* @author seanh
*/
class AutoSelectingTextField extends JTextField implements FocusListener {
private String initialText;
AutoSelectingTextField(String text) {
super(text);
this.initialText = text;
addFocusListener(this);
}
public void focusLost(FocusEvent fe) {
select(0,0);
}
public void focusGained(FocusEvent fe) {
if (getText().equals(initialText)) {
selectAll();
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = f.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JLabel("Click on one text field then the other to focus and autoselect:"));
contentPane.add(new AutoSelectingTextField("Enter your text here"));
contentPane.add(new AutoSelectingTextField("Enter your text here"));
f.pack();
f.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment