void () { FontMetrics metrics = getFontMetrics(getFont()); int width = getSize().width - metrics.stringWidth("..."); if (width <= 0) { setText("..."); setToolTipText(myAllText); } char[] text = myAllText.toCharArray(); int[] charsWidth = new int[text.length]; for (int i = 0; i < text.length; i++) { int w = metrics.charWidth(text[i]); for (int j = 0; j <= i; j++) { charsWidth[i] += w; } } int idx = Arrays.binarySearch(charsWidth, 0, charsWidth.length, width); if (idx < 0) { idx = -idx - 1; } if (idx < myAllText.length()) { setText(myAllText.substring(0, idx) + "..."); setToolTipText(myAllText.substring(idx)); } else { setText(myAllText.substring(0, idx)); } }