Java JTextField Focus clearOnFocus(final JTextField jtf, final String onlyInText)

Here you can find the source of clearOnFocus(final JTextField jtf, final String onlyInText)

Description

clear On Focus

License

LGPL

Declaration

public static void clearOnFocus(final JTextField jtf, final String onlyInText) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextField;

public class Main {

    public static void clearOnFocus(final JTextField jtf, final String onlyInText) {
        jtf.addFocusListener(new FocusListener() {
            @Override/* ww w .j  av a2s  . c om*/
            public void focusGained(FocusEvent e) {
                if (onlyInText != null) {
                    if (jtf.getText().equals(onlyInText)) {
                        jtf.setText("");
                    }
                } else {
                    jtf.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (jtf.getText().isEmpty()) {
                    jtf.setText(onlyInText);
                }
            }
        });
    }
}

Related

  1. _createJTextField(String name, FocusListener fl)
  2. addListenerToCommitOnFocusLost(final JTextField field)
  3. addNewFocusListenerForTextField(final JTextField textField, final Runnable r)
  4. attachListenerOnLabelClickFocusTextElement(JLabel label, final JTextField textField)
  5. configureFocusActionKeys(JTextField textField, boolean primary)
  6. makeSureUserGetsFocus(final JTextField field)
  7. minimoCaracteres(JTextField txt, FocusEvent e, int pValor)