Java Utililty Methods JTextComponent Select

List of utility methods to do JTextComponent Select

Description

The list of methods to do JTextComponent Select are organized into topic(s).

Method

voidaddSelectAllTextOnFocus(final JTextComponent component)
add Select All Text On Focus
component.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent e) {
        component.selectAll();
});
voidfocusSelect(final JTextComponent tf)
focus Select
tf.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent e) {
        tf.selectAll();
});
booleanisSelectionUpperCase(JTextComponent textPane)
is Selection Upper Case
String text = textPane.getSelectedText();
byte[] bytes = text.getBytes();
for (int i = 0; i < bytes.length; i++) {
    if (Character.isLowerCase((char) bytes[i])) {
        return false;
return true;
...
voidreplaceSelectionAndSelect(JTextComponent component, String text)
replace Selection And Select
int position = Math.min(component.getSelectionStart(), component.getSelectionEnd());
component.replaceSelection(text);
component.setCaretPosition(position);
component.moveCaretPosition(position + text.length());
voidselectAll(final JTextComponent textComponent)
Selects all contents of a text component.
if (!textComponent.hasFocus()) {
    textComponent.requestFocus();
textComponent.selectAll();
voidselectAll(JTextComponent textComponent)
select All
textComponent.selectAll();
voidselectAllOnFocus(JTextComponent textComponent)
select All On Focus
textComponent.addFocusListener(SELECT_ALL_FOCUS_LISTENER);
voidselectAndRequestFocus(JTextComponent editor)
select And Request Focus
editor.requestFocusInWindow();
editor.selectAll();
voidselectFirstArg(String strText, int initialSelectionStart, JTextComponent editor)
select First Arg
int firstParen = strText.indexOf("(");
if (firstParen >= 0 && firstParen < strText.indexOf(")")) {
    int startPos = initialSelectionStart + firstParen + 1;
    editor.setCaretPosition(startPos);
voidselectionToUpperCase(JTextComponent textPane)
selection To Upper Case
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
if (Math.abs(start - end) > 0) {
    String upper = textPane.getSelectedText().toUpperCase();
    textPane.replaceSelection(upper);
    textPane.setCaretPosition(start);
    textPane.moveCaretPosition(end);