Here you can find the source of selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)
public static void selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)
//package com.java2s; //License from project: LGPL import java.awt.Color; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JTextField; public class Main { public static void selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent) { jtf.addFocusListener(new FocusListener() { @Override//from ww w. ja v a2 s . c om public void focusLost(FocusEvent e) { if (recoverText != null) { String text = jtf.getText(); if (text.isEmpty()) { jtf.setForeground(forTip); jtf.setText(recoverText); } else if (text.equals(recoverText)) { jtf.setForeground(forTip); } } } @Override public void focusGained(FocusEvent e) { jtf.selectAll(); jtf.setForeground(forContent); } }); jtf.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2 && recoverText != null && jtf.getText().equals(recoverText)) { jtf.setText(""); } } }); } }