public static void setAppropriateFont(final JLabel label) { final String text = label.getText(); if (text == null) { return; } Font font = label.getFont(); final int canDisplay = font.canDisplayUpTo(text); if (canDisplay != -1) { logger.info("Default font ('{}') was unable to display text ('{}'), searching for alternative.", font.getName(), text); // if the label contains undisplayable characters, look for a // different font able of displaying the characters. font = findCompatibleFont(text, font); label.setFont(font); } }
private static <E> Function<E, Font> createFontTransformer() { return input -> { final Font defaultFont = WidgetUtils.FONT_SMALL; if (input == null) { return defaultFont; } final String str; if (input instanceof HasName) { str = ((HasName) input).getName(); } else if (input instanceof Object[]) { str = Arrays.toString((Object[]) input); } else { str = input.toString(); } if (defaultFont.canDisplayUpTo(str) == -1) { return defaultFont; } final Font findCompatibleFont = WidgetUtils.findCompatibleFont(str, WidgetUtils.FONT_SMALL); return findCompatibleFont.deriveFont(WidgetUtils.FONT_SIZE_SMALL); }; } }