Here you can find the source of getStringWidth(String loginID, Font font)
public static int getStringWidth(String loginID, Font font)
//package com.java2s; /*//w w w . j a va2s. com * Copyright (C) 2016 Open Source Parking, Inc.(www.osparking.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; public class Main { public static int getStringWidth(String loginID, Font font) { AffineTransform affinetransform = new AffineTransform(); FontRenderContext frc = new FontRenderContext(affinetransform, true, true); return (int) ((font.getStringBounds(loginID, frc)).getWidth()); } }