Java JComponent Width clipStringifNeeded(final JComponent component, final String string, final int avaiableWidth)

Here you can find the source of clipStringifNeeded(final JComponent component, final String string, final int avaiableWidth)

Description

Clips a string if its longer than the specified width.

License

Open Source License

Parameter

Parameter Description
component Component containing string
string String to check
avaiableWidth Available Width

Return

String (clipped if required)

Declaration

public static String clipStringifNeeded(final JComponent component, final String string,
        final int avaiableWidth) 

Method Source Code

//package com.java2s;
/*/* w  w  w . ja  v  a2 s . c  o  m*/
 * Copyright (c) 2006-2017 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.awt.FontMetrics;

import javax.swing.JComponent;

import javax.swing.SwingUtilities;

public class Main {
    /**
     * Clips a string if its longer than the specified width.
     *
     * @param component     Component containing string
     * @param string        String to check
     * @param avaiableWidth Available Width
     *
     * @return String (clipped if required)
     */
    public static String clipStringifNeeded(final JComponent component, final String string,
            final int avaiableWidth) {
        if (string == null || string.isEmpty()) {
            return "";
        }
        final FontMetrics fm = component.getFontMetrics(component.getFont());
        final int width = SwingUtilities.computeStringWidth(fm, string);
        if (width > avaiableWidth) {
            return clipString(component, string, avaiableWidth);
        }
        return string;
    }

    /**
     * Clips the passed string .
     *
     * @param component     Component containing string
     * @param string        String to check
     * @param avaiableWidth Available Width
     *
     * @return String (clipped if required)
     */
    public static String clipString(final JComponent component, final String string, final int avaiableWidth) {
        if (string == null || string.isEmpty()) {
            return "";
        }
        final FontMetrics fm = component.getFontMetrics(component.getFont());
        final String clipString = "...";
        int width = SwingUtilities.computeStringWidth(fm, clipString);

        int nChars = 0;
        for (final int max = string.length(); nChars < max; nChars++) {
            width += fm.charWidth(string.charAt(nChars));
            if (width > avaiableWidth) {
                break;
            }
        }
        return string.substring(0, nChars) + clipString;
    }
}

Related

  1. adjustWidth(JComponent aComponent, int aWidth)
  2. allignBottom(JComponent lead, JComponent target, int width, int height)
  3. calculateCharWidth(JComponent component, int numChars)
  4. clearWidth(JComponent comp)
  5. clipString(final JComponent component, final String string, final int avaiableWidth)
  6. createTitledPanel(JComponent component, String title, int width, int height)
  7. fixWidth(JComponent c, int width)
  8. getMinimumWidth(JComponent comp)
  9. getRectangle(JComponent comp)