Here you can find the source of toggleSelectionUpperCase(JTextComponent textPane)
public static void toggleSelectionUpperCase(JTextComponent textPane)
//package com.java2s; //License from project: Apache License import javax.swing.text.JTextComponent; public class Main { public static void toggleSelectionUpperCase(JTextComponent textPane) { if (isSelectionUpperCase(textPane)) { selectionToLowerCase(textPane); } else {/*from w ww .j a va 2s . c o m*/ selectionToUpperCase(textPane); } } public static boolean isSelectionUpperCase(JTextComponent textPane) { 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; } public static void selectionToLowerCase(JTextComponent textPane) { int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); if (Math.abs(start - end) > 0) { String lower = textPane.getSelectedText().toLowerCase(); textPane.replaceSelection(lower); textPane.setCaretPosition(start); textPane.moveCaretPosition(end); } } public static void selectionToUpperCase(JTextComponent textPane) { 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); } } }