Java Utililty Methods JTextField Focus

List of utility methods to do JTextField Focus

Description

The list of methods to do JTextField Focus are organized into topic(s).

Method

JTextField_createJTextField(String name, FocusListener fl)
create J Text Field
JTextField jt = new JTextField();
jt.setName(name);
jt.addFocusListener(fl);
return jt;
voidaddListenerToCommitOnFocusLost(final JTextField field)
Add Focus listener to text field to commit field whenever it loses focus.
field.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent evt) {
        field.postActionEvent();
});
voidaddNewFocusListenerForTextField(final JTextField textField, final Runnable r)
add New Focus Listener For Text Field
textField.addFocusListener(new FocusListener() {
    @Override
    public void focusLost(final FocusEvent arg0) {
        r.run();
    @Override
    public void focusGained(final FocusEvent arg0) {
        textField.selectAll();
...
voidattachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField)
Attaches a listener to the specified label that directs the focus to the supplied text (resulting in an HTML form-like connection of the label and its input field).
label.addMouseListener(new MouseInputAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        focusAccompanyingInput();
    @Override
    public void mousePressed(MouseEvent e) {
        focusAccompanyingInput();
...
voidclearOnFocus(final JTextField jtf, final String onlyInText)
clear On Focus
jtf.addFocusListener(new FocusListener() {
    @Override
    public void focusGained(FocusEvent e) {
        if (onlyInText != null) {
            if (jtf.getText().equals(onlyInText)) {
                jtf.setText("");
        } else {
...
voidconfigureFocusActionKeys(JTextField textField, boolean primary)
configure Focus Action Keys
if (!primary) {
    textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), JTextField.notifyAction);
    textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JTextField.notifyAction);
textField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
textField.addFocusListener(new FocusListener() {
    @Override
    public void focusGained(FocusEvent e) {
...
voidmakeSureUserGetsFocus(final JTextField field)
Forces focus to a particular JTextField in a displayed dialog
field.addHierarchyListener(new HierarchyListener() {
    HierarchyListener hierarchyListener = this;
    @Override
    public void hierarchyChanged(HierarchyEvent e) {
        JRootPane rootPane = SwingUtilities.getRootPane(field);
        if (rootPane != null) {
            final JButton okButton = rootPane.getDefaultButton();
            if (okButton != null) {
...
booleanminimoCaracteres(JTextField txt, FocusEvent e, int pValor)
minimo Caracteres
if (txt.getText().length() <= pValor) {
    return true;
return false;