// this depends on who has the focus right now.
final Component owner = FocusManager.getCurrentManager().getPermanentFocusOwner();
if (owner instanceof JTextComponent)
{
final JTextComponent textComponent = (JTextComponent) owner;
textComponent.select(textComponent.getCaretPosition(), textComponent.getCaretPosition());
}
else if (owner instanceof TextComponent)
{
final TextComponent textComponent = (TextComponent) owner;
textComponent.select(textComponent.getCaretPosition(), textComponent.getCaretPosition());