Java Code Examples for javax.swing.JComponent.getFontMetrics()

The following are Jave code examples for showing how to use getFontMetrics() of the javax.swing.JComponent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: bug8132119.java   View Source Code Vote up 6 votes
private static void testStringWidth() {

        String str = "12345678910\u036F";
        JComponent comp = createComponent(str);
        Font font = comp.getFont();
        FontMetrics fontMetrics = comp.getFontMetrics(font);
        float stringWidth = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        if (stringWidth == fontMetrics.stringWidth(str)) {
            throw new RuntimeException("Numeric shaper is not used!");
        }

        if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) {
            throw new RuntimeException("Wrong text width!");
        }
    }
 
Example 2
Project: openjdk-jdk10   File: bug8132119.java   View Source Code Vote up 6 votes
private static void testStringClip() {

        String str = "1234567890";
        JComponent comp = createComponent(str);
        FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());

        int width = (int) BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        String clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width);
        checkClippedString(str, clip, str);

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width + 1);
        checkClippedString(str, clip, str);

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, -1);
        checkClippedString(str, clip, "...");

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, 0);
        checkClippedString(str, clip, "...");

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics,
                str, width - width / str.length());
        int endIndex = str.length() - 3;
        checkClippedString(str, clip, str.substring(0, endIndex) + "...");
    }
 
Example 3
Project: openjdk-jdk10   File: bug8132119.java   View Source Code Vote up 6 votes
private static void checkNullArgumentsGetClippedString(
        JComponent comp, String text) {

    FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());

    BasicGraphicsUtils.getClippedString(null, fontMetrics, text, 1);
    String result = BasicGraphicsUtils.getClippedString(comp, fontMetrics, null, 1);
    if (!"".equals(result)) {
        throw new RuntimeException("Empty string is not returned!");
    }

    try {
        BasicGraphicsUtils.getClippedString(comp, null, text, 1);
    } catch (NullPointerException e) {
        return;
    }

    throw new RuntimeException("NPE is not thrown");
}
 
Example 4
Project: openjdk-jdk10   File: bug8132119.java   View Source Code Vote up 6 votes
private static void checkNullArgumentsGetStringWidth(JComponent comp,
        String text) {

    FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
    BasicGraphicsUtils.getStringWidth(null, fontMetrics, text);
    float result = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, null);

    if (result != 0) {
        throw new RuntimeException("The string length is not 0");
    }

    try {
        BasicGraphicsUtils.getStringWidth(comp, null, text);
    } catch (NullPointerException e) {
        return;
    }

    throw new RuntimeException("NPE is not thrown");
}
 
Example 5
Project: incubator-netbeans   File: AbstractViewTabDisplayerUI.java   View Source Code Vote up 5 votes
protected final FontMetrics getTxtFontMetrics() {
    if (fm == null) {
        JComponent control = getDisplayer();
        fm = control.getFontMetrics(getTxtFont());
    }
    return fm;
}
 
Example 6
Project: incubator-netbeans   File: QuickSearchPopup.java   View Source Code Vote up 5 votes
/** Computes width of string up to maxCharCount, with font of given JComponent
 * and with maximum percentage of owning Window that can be taken */
private static int computeWidth (JComponent comp, int maxCharCount, int percent) {
    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    int charW = fm.charWidth('X');
    int result = charW * maxCharCount;
    // limit width to 50% of containing window
    Window w = SwingUtilities.windowForComponent(comp);
    if (w != null) {
        result = Math.min(result, w.getWidth() * percent / 100);
    }
    return result;
}
 
Example 7
Project: incubator-netbeans   File: DashboardUtils.java   View Source Code Vote up 5 votes
public static String computeFitText(JComponent component, int maxWidth, String text, boolean bold) {
    if (text == null) {
        text = ""; // NOI18N
    }
    if (text.length() <= VISIBLE_START_CHARS + 3) {
        return text;
    }
    FontMetrics fm;
    if (bold) {
        fm = component.getFontMetrics(component.getFont().deriveFont(Font.BOLD));
    } else {
        fm = component.getFontMetrics(component.getFont());
    }
    int width = maxWidth;

    String sufix = "..."; // NOI18N
    int sufixLength = fm.stringWidth(sufix + " "); //NOI18N
    int desired = width - sufixLength;
    if (desired <= 0) {
        return text;
    }

    for (int i = 0; i <= text.length() - 1; i++) {
        String prefix = text.substring(0, i);
        int swidth = fm.stringWidth(prefix);
        if (swidth >= desired) {
            if (fm.stringWidth(text.substring(i + 1)) <= fm.stringWidth(sufix)) {
                return text;
            }
            return prefix.length() > 0 ? prefix + sufix : text;
        }
    }
    return text;
}
 
Example 8
Project: incubator-netbeans   File: QuickSearchPopup.java   View Source Code Vote up 5 votes
/** Computes width of string up to maxCharCount, with font of given JComponent
 * and with maximum percentage of owning Window that can be taken */
private static int computeWidth (JComponent comp, int maxCharCount, int percent) {
    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    int charW = fm.charWidth('X');
    int result = charW * maxCharCount;
    // limit width to 50% of containing window
    Window w = SwingUtilities.windowForComponent(comp);
    if (w != null) {
        result = Math.min(result, w.getWidth() * percent / 100);
    }
    return result;
}
 
Example 9
Project: openjdk-jdk10   File: bug8132119.java   View Source Code Vote up 5 votes
private static void testDrawString(boolean underlined) {
    String str = "AOB";
    JComponent comp = createComponent(str);

    BufferedImage buffImage = createBufferedImage(WIDTH, HEIGHT);
    Graphics2D g2 = buffImage.createGraphics();

    g2.setColor(DRAW_COLOR);
    g2.setFont(comp.getFont());

    FontMetrics fontMetrices = comp.getFontMetrics(comp.getFont());
    float width = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, str);
    float x = (WIDTH - width) / 2;
    int y = 3 * HEIGHT / 4;

    if (underlined) {
        BasicGraphicsUtils.drawStringUnderlineCharAt(comp, g2, str, 1, x, y);
    } else {
        BasicGraphicsUtils.drawString(comp, g2, str, x, y);
    }
    g2.dispose();

    float xx = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "A") +
            BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "O")/2;

    checkImageContainsSymbol(buffImage, (int) xx, underlined ? 3 : 2);
}
 
Example 10
Project: incubator-netbeans   File: UIUtils.java   View Source Code Vote up 4 votes
public static int getColumnWidthInPixels(String str, JComponent comp) {
    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    return fm.stringWidth(str);
}