Here you can find the source of clearOnFocus(final JTextField jtf, final String onlyInText)
public static void clearOnFocus(final JTextField jtf, final String onlyInText)
//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); } } }); } }