Java Code Examples for javax.swing.JLabel#getFontMetrics()

The following examples show how to use javax.swing.JLabel#getFontMetrics() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
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
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
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
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 Project: SmartIM   File: PopupPanel.java    License: Apache License 2.0 6 votes vote down vote up
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 6
Source Project: netbeans   File: QueryTableCellRenderer.java    License: Apache License 2.0 6 votes vote down vote up
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 7
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
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
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 Project: hottub   File: KerningLeak.java    License: GNU General Public License v2.0 6 votes vote down vote up
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
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
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 Project: pumpernickel   File: ThumbnailLabelUI.java    License: MIT License 6 votes vote down vote up
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 14
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 15
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
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 Project: swift-explorer   File: ProgressPanel.java    License: Apache License 2.0 5 votes vote down vote up
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 Project: swing_library   File: TableRowUtilities.java    License: MIT License 5 votes vote down vote up
/**
 * 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
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 Project: FoxBPM   File: SVGUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 根据文本的式样,以及文本内容,获取文本在屏幕上展示的像素宽
 * 
 * @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());
}