List of utility methods to do Swing Font Size
int | calculateTextSize(final String text, final int fontSize) Calculate text size. final JLabel lbl = new JLabel(text); return (int) lbl.getPreferredSize().getWidth(); |
JLabel | genSizedLabel(String text, float fontSize) gen Sized Label JLabel label = new JLabel(text, JLabel.CENTER); label.setFont(label.getFont().deriveFont(fontSize)); return label; |
int | getBaseFontSize() 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(); ... |
Border | getCheckBoxBorder(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); ... |
int | getDefaultFontSize() 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; |
int | getFontRelativeSize(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(); |
float | getFontSizeFactor() 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(); ... |
Dimension | getSizeOfTextInComponent(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()); |
void | increaseFontSize(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));
|
int | printDocumentMonospaced(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; ... |