Here you can find the source of selectAndFocus(JComponent component)
Parameter | Description |
---|---|
component | the text component |
public static void selectAndFocus(JComponent component)
//package com.java2s; /*/*from www . j a v a 2 s . co m*/ * SwingHelper.java * This file is part of Portecle, a multipurpose keystore and certificate tool. * * Copyright ? 2007-2014 Ville Skytt?, ville.skytta@iki.fi * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ import java.awt.Component; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.text.JTextComponent; public class Main { /** * Select all text in a text component and focus it. * * @param component the text component */ public static void selectAndFocus(JComponent component) { JTextComponent textComponent = null; if (component instanceof JTextComponent) { textComponent = (JTextComponent) component; } if (component instanceof JComboBox) { Component editorComponent = ((JComboBox<?>) component).getEditor().getEditorComponent(); if (editorComponent instanceof JTextComponent) { textComponent = (JTextComponent) editorComponent; } } if (textComponent != null) { textComponent.select(0, textComponent.getText().length()); } component.requestFocusInWindow(); } }