Example usage for javax.swing JLabel getFontMetrics

List of usage examples for javax.swing JLabel getFontMetrics

Introduction

In this page you can find the example usage for javax.swing JLabel getFontMetrics.

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300, 300);//from w w  w .j  a  va2  s  . co  m

    JPanel panel = new JPanel(new GridLayout(3, 1));
    JLabel label = new JLabel();
    JTextField tf = new JTextField();
    JButton b = new JButton("calc sting width");
    b.addActionListener(e -> {
        FontMetrics fm = label.getFontMetrics(label.getFont());
        String text = tf.getText();
        int textWidth = fm.stringWidth(text);
        label.setText("text width for \"" + text + "\": " + textWidth);
    });
    panel.add(label);
    panel.add(tf);
    panel.add(b);
    frame.setContentPane(panel);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * "Fixates" the preferred width of the given label to the given text.
 * /* ww w. j  av a2 s .c  o  m*/
 * @param aLabel
 *          the label to fixate, cannot be <code>null</code>;
 * @param aMinimalText
 *          the text to use as minimal width indicator.
 */
public static final void fixLabelWidth(final JLabel aLabel, final String aMinimalText) {
    final FontMetrics fm = aLabel.getFontMetrics(aLabel.getFont());
    final int height = fm.getHeight();

    aLabel.setPreferredSize(new Dimension(fm.stringWidth(aMinimalText), height));
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Gets the width of a given label.//w w w.  j  ava 2s.  com
 */
public static int width(JLabel c) {
    FontMetrics fm = c.getFontMetrics(c.getFont());
    return fm.stringWidth(c.getText()) + 3;
}

From source file:org.jdal.swing.form.SimpleBoxFormBuilder.java

/**
 * Add a component with label, increments cursor by two.
 * @param name label string/*from   w ww.  jav a  2  s.co  m*/
 * @param c component.
 */
public void add(String name, Component c) {
    JLabel label = new JLabel(name);
    add(label);
    Rectangle2D rec = label.getFontMetrics(label.getFont()).getStringBounds(name, container.getGraphics());
    setMaxWidth(rec.getBounds().width + 10);
    add(c);
}

From source file:SuitaDetails.java

private void initComponents(ArrayList<String[]> descriptions) {
    global = new JPanel();
    global.setBackground(Color.WHITE);
    initGlobal();/*from  w  ww .j a  v  a  2 s  .  c o m*/
    initTCOptions();
    initSummary();
    definitions.clear();
    border = BorderFactory.createTitledBorder("Global options");
    setBorder(border);
    scroll = new JScrollPane();
    //         setMinimumSize(new Dimension(10,10));
    //         setMaximumSize(new Dimension(1000,1000));
    //         setPreferredSize(new Dimension(100,100));
    defsContainer = new JPanel();
    setLayout(new BorderLayout());
    defsContainer.setBackground(Color.WHITE);
    defsContainer.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
    defsContainer.setLayout(new BoxLayout(defsContainer, BoxLayout.Y_AXIS));
    defsContainer.add(suiteoptions);
    scroll.setViewportView(global);
    add(scroll, BorderLayout.CENTER);
    JLabel l = new JLabel("test");
    FontMetrics metrics = l.getFontMetrics(l.getFont());
    int width = 0;
    for (int i = 0; i < descriptions.size(); i++) {
        if (width < metrics.stringWidth(descriptions.get(i)[RunnerRepository.LABEL])) {
            width = metrics.stringWidth(descriptions.get(i)[RunnerRepository.LABEL]);
        }
    }
    for (int i = 0; i < descriptions.size(); i++) {
        String button = descriptions.get(i)[RunnerRepository.SELECTED];
        DefPanel define = new DefPanel(descriptions.get(i)[RunnerRepository.LABEL], button,
                descriptions.get(i)[RunnerRepository.ID], width, i, this);
        definitions.add(define);
        defsContainer.add(define);
    }
}

From source file:org.foxbpm.engine.impl.diagramview.svg.SVGUtils.java

/**
 * ??????//from www.ja  v a2 s.co m
 * 
 * @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());
}

From source file:org.jas.dnd.DragTooltipDialog.java

private DynamicPanel getDynamicPanel(IconType type, List<?> list) {
    if (list == null || list.isEmpty()) {
        return null;
    }/*from  www. j  a  v a 2s . c  o m*/

    JPanel dynamicPanel = null;
    JLabel dynamicText = null;

    String text = list.isEmpty() ? null : (list.size()) + " " + (type == null ? "ERROR" : type.getText());
    if (list.size() == 1) {
        text = list.get(0) instanceof File ? ((File) list.get(0)).getName() : list.get(0).toString();
    }

    dynamicText = new JLabel(text);
    dynamicText.setForeground(Color.WHITE);
    FontMetrics fontMetrics = dynamicText.getFontMetrics(dynamicText.getFont());
    int width = fontMetrics.stringWidth(dynamicText.getText()) + DEFAULT_MIN_FONT_WIDTH;
    int realHeight = ROW_HEIGHT;
    String longestText = "";
    while (text.contains("<br>")) {
        text = text.substring(text.indexOf("<br>") + DEFAULT_MIN_FONT_WIDTH);
        if (text.length() > longestText.length()) {
            longestText = text;
        }
        realHeight += ROW_HEIGHT;
    }
    if (!longestText.isEmpty()) {
        width = fontMetrics.stringWidth(longestText) + DEFAULT_MIN_FONT_WIDTH;
    }
    dynamicPanel = new JPanel();
    dynamicPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    dynamicPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, realHeight));
    dynamicPanel.setMinimumSize(new Dimension(0, realHeight));
    dynamicPanel.setPreferredSize(new Dimension(100, realHeight));
    dynamicPanel.setSize(new Dimension(100, realHeight));
    if (type != null) {
        dynamicPanel.add(getDynamicIcon(type), null);
        JPanel spacer = new JPanel();
        Dimension d = new Dimension(SPACER_WIDTH, SPACER_WIDTH);
        width += SPACER_WIDTH;

        spacer.setSize(d);
        spacer.setMinimumSize(d);
        spacer.setMaximumSize(d);
        spacer.setPreferredSize(d);
        dynamicPanel.add(spacer);
        width += type.width;
    }
    dynamicPanel.add(dynamicText);
    return new DynamicPanel(dynamicPanel, width, realHeight);
}

From source file:org.nuclos.client.ui.JInfoTabbedPane.java

private void setTabComponent(String text, int index) {
    JLabel tabComponent = new JLabel(text);
    int height = tabComponent.getFontMetrics(tabComponent.getFont()).getHeight();
    int width = tabComponent.getFontMetrics(tabComponent.getFont()).stringWidth(text + " (>999)");
    tabComponent.setPreferredSize(new Dimension(width, height));
    tabComponent.setHorizontalAlignment(SwingConstants.CENTER);
    setTabComponentAt(index, tabComponent);
}

From source file:pl.edu.icm.visnow.system.main.VisNow.java

private void profile() {
    JLabel jl = new JLabel("VisNow");
    jl.setFont(new java.awt.Font("Tahoma", 1, 9));
    jl.getFontMetrics(jl.getFont()).charsWidth(jl.getText().toCharArray(), 0, jl.getText().length());
}

From source file:pt.webdetails.cgg.scripts.BaseScope.java

public static Object getTextLenCGG(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    String text = Context.toString(args[0]);
    String fontFamily = Context.toString(args[1]);
    String fontSize = Context.toString(args[2]).trim();
    String fontStyle = "normal";
    String fontWeight = "normal";

    if (args.length > 3) {
        fontStyle = Context.toString(args[3]);

        if (args.length > 4) {
            fontWeight = Context.toString(args[4]);
        }/*from w w  w .ja va 2  s  .c  o  m*/
    }

    Font ffont = getFont(fontFamily, fontSize, fontStyle, fontWeight);

    JLabel label = new JLabel();

    FontMetrics fMetric = label.getFontMetrics(ffont);

    int width = fMetric.stringWidth(text);

    return Context.toNumber(width);
}