Get Char width based on current font in Java
Description
The following code shows how to get Char width based on current font.
Example
/* w w w . ja v a2s . c o m*/
import java.awt.FontMetrics;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setVisible(true);
FontMetrics metrics = f.getFontMetrics(f.getFont());
int widthX = metrics.charWidth('X');
System.out.println(widthX);
}
}
The code above generates the following result.