Java Utililty Methods Swing Font Size

List of utility methods to do Swing Font Size

Description

The list of methods to do Swing Font Size are organized into topic(s).

Method

intcalculateTextSize(final String text, final int fontSize)
Calculate text size.
final JLabel lbl = new JLabel(text);
return (int) lbl.getPreferredSize().getWidth();
JLabelgenSizedLabel(String text, float fontSize)
gen Sized Label
JLabel label = new JLabel(text, JLabel.CENTER);
label.setFont(label.getFont().deriveFont(fontSize));
return label;
intgetBaseFontSize()
Returns the base font size for the Nimbus Look and Feel

Assumes that the Nimbus look and feel has been set.

int baseSize = 12;
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
Object object = defaults.get("Label.font");
if (object != null) {
    if (object instanceof Font) {
        baseSize = ((Font) object).getSize();
    } else if (object instanceof FontUIResource) {
        baseSize = ((FontUIResource) object).getSize();
...
BordergetCheckBoxBorder(int fontSize, boolean ltr)
Returns the border for check boxes under the specified font size.
int tInset = getAdjustedSize(fontSize, 2, 3, 1, false);
int bInset = getAdjustedSize(fontSize, 3, 3, 1, false);
if (fontSize == 11) {
    tInset = 2;
    bInset = 2;
int leadingInset = getAdjustedSize(fontSize, 3, 3, 1, false);
int trailingInset = getAdjustedSize(fontSize, 5, 3, 1, false);
...
intgetDefaultFontSize()
get Default Font Size
javax.swing.UIDefaults uiDefaults = javax.swing.UIManager.getDefaults();
Object value = uiDefaults.get("defaultFont");
if (value instanceof javax.swing.plaf.FontUIResource) {
    javax.swing.plaf.FontUIResource fontUIResource = (javax.swing.plaf.FontUIResource) value;
    return fontUIResource.getSize();
} else {
    return 12;
intgetFontRelativeSize(int size)
get Font Relative Size
StringBuilder sb = new StringBuilder(size);
for (int i = 0; i < size; i++) {
    sb.append("W");
JLabel lbl = new JLabel(sb.toString());
return (int) lbl.getPreferredSize().getWidth();
floatgetFontSizeFactor()
Get a factor of the difference between the default font size NetBeans uses, and the actual font size which may be different if the -fontsize argument was used on startup.
if (fsfactor == -1) {
    Font f = UIManager.getFont("controlFont"); 
    if (f == null) {
        JLabel jl = new JLabel();
        f = jl.getFont();
    int baseSize = 12; 
    fsfactor = baseSize / f.getSize();
...
DimensiongetSizeOfTextInComponent(String text, Font font, JComponent component)
returns the size of the text
FontMetrics fontMetrics = SwingUtilities2.getFontMetrics(component, font);
return new Dimension(fontMetrics.stringWidth(text), fontMetrics.getHeight());
voidincreaseFontSize(JComponent component, int offset)
Modifica fontul de la un label si il face mai mare
Font oldFont = component.getFont();
if (oldFont != null)
    component.setFont(oldFont.deriveFont(oldFont.getStyle(), oldFont.getSize() + offset));
intprintDocumentMonospaced(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize)
Prints a Document using a monospaced font, and does no word wrapping (ie, words will wrap mid-word to the next line).
g.setColor(Color.BLACK);
g.setFont(new Font("Monospaced", Font.PLAIN, fontSize));
tabSizeInSpaces = tabSize;
fm = g.getFontMetrics();
int fontWidth = fm.charWidth('w'); 
int fontHeight = fm.getHeight();
int MAX_CHARS_PER_LINE = (int) pageFormat.getImageableWidth() / fontWidth;
int MAX_LINES_PER_PAGE = (int) pageFormat.getImageableHeight() / fontHeight;
...