Java Swing Tutorial - Java FontMetrics.charsWidth(char[] data, int off, int len)








Syntax

FontMetrics.charsWidth(char[] data, int off, int len) has the following syntax.

public int charsWidth(char[] data,  int off,  int len)

Example

In the following code shows how to use FontMetrics.charsWidth(char[] data, int off, int len) method.

/*from  www .  j a va 2 s .  c om*/
import java.awt.FontMetrics;

import javax.swing.JFrame;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    f.setSize(300, 200);
    f.setVisible(true);
    
    FontMetrics metrics = f.getFontMetrics(f.getFont());

    int widthX = metrics.charsWidth(new char[]{'a','b'}, 1,2);

    System.out.println(widthX);
    
  }
}