Java Code Examples for javax.swing.JLabel#getFontMetrics()
The following examples show how to use
javax.swing.JLabel#getFontMetrics() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KerningLeak.java From dragonwell8_jdk with GNU General Public License v2.0 | 7 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 2
Source File: KerningLeak.java From jdk8u-jdk with GNU General Public License v2.0 | 7 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 3
Source File: KerningLeak.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 4
Source File: KerningLeak.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 5
Source File: KerningLeak.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 6
Source File: ThumbnailLabelUI.java From pumpernickel with MIT License | 6 votes |
protected void calculateGeometry(JLabel label) { FontMetrics fm = label.getFontMetrics(label.getFont()); String text = label.getText(); Icon icon = label.getIcon(); int verticalAlignment = label.getVerticalAlignment(); int horizontalAlignment = label.getHorizontalAlignment(); int verticalTextPosition = label.getVerticalTextPosition(); int horizontalTextPosition = label.getHorizontalTextPosition(); int textIconGap = label.getIconTextGap(); viewR.setFrame(0, 0, getViewWidth(label), label.getHeight()); SwingUtilities.layoutCompoundLabel(fm, text, icon, verticalAlignment, horizontalAlignment, verticalTextPosition, horizontalTextPosition, viewR, iconRect, textRect, textIconGap); textRect.x = label.getWidth() / 2 - textRect.width / 2; iconRect.x = label.getWidth() / 2 - iconRect.width / 2; iconRect.y = 0; textRect.y = iconRect.height + label.getIconTextGap(); }
Example 7
Source File: KerningLeak.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 8
Source File: KerningLeak.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 9
Source File: KerningLeak.java From hottub with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 10
Source File: KerningLeak.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 11
Source File: KerningLeak.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 12
Source File: KerningLeak.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 13
Source File: QueryTableCellRenderer.java From netbeans with Apache License 2.0 | 6 votes |
private static String computeFitText(JLabel label) { String text = label.getText(); if(text == null) text = ""; if (text.length() <= VISIBLE_START_CHARS + 3) return text; Icon icon = label.getIcon(); int iconWidth = icon != null ? icon.getIconWidth() : 0; FontMetrics fm = label.getFontMetrics(label.getFont()); int width = label.getSize().width - iconWidth; String sufix = "..."; // NOI18N int sufixLength = fm.stringWidth(sufix); 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) { return prefix.length() > 0 ? prefix + sufix: text; } } return text; }
Example 14
Source File: PopupPanel.java From SmartIM with Apache License 2.0 | 6 votes |
void setLabelText(JLabel label, String text) { StringBuilder builder = new StringBuilder("<html>"); char[] chars = text.toCharArray(); FontMetrics fontMetrics = label.getFontMetrics(label.getFont()); int start = 0; int len = 0; while (start + len < text.length()) { while (true) { len++; if (start + len > text.length()) break; if (fontMetrics.charsWidth(chars, start, len) > label.getWidth()) { break; } } builder.append(chars, start, len - 1).append("<br/>"); start = start + len - 1; len = 0; } builder.append(chars, start, text.length() - start); builder.append("</html>"); label.setText(builder.toString()); }
Example 15
Source File: KerningLeak.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 16
Source File: KerningLeak.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private static void leak() { Map<TextAttribute, Object> textAttributes = new HashMap<>(); textAttributes.put(TextAttribute.FAMILY, "Sans Serif"); textAttributes.put(TextAttribute.SIZE, 12); textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font font = Font.getFont(textAttributes); JLabel label = new JLabel(); int dummy = 0; for (int i = 0; i < 500; i++) { if (i % 10 == 0) System.out.println("Starting iter " + (i+1)); for (int j = 0; j <1000; j++) { FontMetrics fm = label.getFontMetrics(font); dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j)); } } System.out.println("done " + dummy); }
Example 17
Source File: ProgressPanel.java From swift-explorer with Apache License 2.0 | 5 votes |
private String processTextToFit (JLabel lbl, String text) { if (text == null) return "" ; FontMetrics fm = lbl.getFontMetrics(lbl.getFont()); if (fm == null) return text ; int textWidth = fm.stringWidth(text); int lblWidth = lbl.getWidth() ; if (lblWidth >= textWidth) return text ; // we assume that the set of all characters have a mean length equal or smaller than 'Aa' / 2 String dots = " ... " ; int charWidth = fm.stringWidth("Aa") / 2; int dotsWidth = fm.stringWidth(dots); int maxNumChar = (lblWidth - dotsWidth) / charWidth ; int blockWidth = maxNumChar / 2 ; if (blockWidth <= 0) return text ; StringBuilder sb = new StringBuilder () ; sb.append(text.substring(0, blockWidth)) ; sb.append(dots) ; sb.append(text.substring(text.length() - blockWidth)) ; return sb.toString() ; }
Example 18
Source File: TableRowUtilities.java From swing_library with MIT License | 5 votes |
/** * Adjusts the column width of the row headers table containg the number * column. The font metrics are extracted from the label of the row at the * bottom of the viewport and used to determing the appropriate width. * * The reason why this method is important, is that when the row number increases by an extra digit * the column needs to get wider. It also needs to shrink when scrolling to smaller digit numbers. * * @param rowHeadersTable - single column table in the row header * @param label - label used to get font metrics * @param scrollBarValue - int value for determing point of lowest row */ private static void adjustColumnWidth(final JTable rowHeadersTable, final JLabel label, int scrollBarValue) { label.setFont(rowHeadersTable.getFont()); label.setOpaque(true); label.setHorizontalAlignment(JLabel.CENTER); int v = rowHeadersTable.getVisibleRect().height; int row = rowHeadersTable.rowAtPoint(new Point(0, v + scrollBarValue)); Integer modelValue = null; if (row != -1) { modelValue = (Integer) rowHeadersTable.getModel().getValueAt(row, 0); } else { RowHeadersTableModel tm = (RowHeadersTableModel) rowHeadersTable.getModel(); modelValue = new Integer(tm.getMaxIntValue()); } label.setText("" + modelValue); FontMetrics fontMetrics = label.getFontMetrics(label.getFont()); int widthFactor = 0; if (fontMetrics != null && label.getText() != null) { widthFactor = fontMetrics.stringWidth(label.getText()); rowHeadersTable.setPreferredScrollableViewportSize(new Dimension(widthFactor + 8, 100)); // height // is // ignored rowHeadersTable.repaint(); } }
Example 19
Source File: StringCellRenderer.java From knopflerfish.org with BSD 3-Clause "New" or "Revised" License | 4 votes |
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) { JLabel label = (JLabel)super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column); String s = obj != null ? obj.toString() : ""; int w = table.getTableHeader().getColumnModel().getColumn(column).getWidth(); FontMetrics fm = label.getFontMetrics(label.getFont()); int fmW = fm.stringWidth(s); // s = w + "/" + fmW + ":" + s; int nChars = 3; int m = s.length() / 2; String s2 = s; while((fmW+10) > w && s2.length() > nChars && s2.length() > 5) { int n1 = nChars / 2; int n2 = nChars - n1; String left = s.substring(0, m - n1); String right = s.substring(m + n2); s2 = left + "..." + right; fmW = fm.stringWidth(s2); nChars++; } label.setText(s2); return label; }
Example 20
Source File: SVGUtils.java From FoxBPM with Apache License 2.0 | 3 votes |
/** * 根据文本的式样,以及文本内容,获取文本在屏幕上展示的像素宽 * * @param font * 字体式样 * @param text * 文本 * @return 文本宽度 */ public final static int getTextWidth(Font font, String text) { JLabel label = new JLabel(text); label.setFont(font); FontMetrics metrics = label.getFontMetrics(label.getFont()); return metrics.stringWidth(label.getText()); }