Java Swing Tutorial - Java FontMetrics.charWidth(int codePoint)








Syntax

FontMetrics.charWidth(int codePoint) has the following syntax.

public int charWidth(int codePoint)

Example

In the following code shows how to use FontMetrics.charWidth(int codePoint) method.

// ww  w  . j av a2s. com
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.charWidth((int)'C');

    System.out.println(widthX);
    
  }
}