Here you can find the source of getStringDimension(Component comp, String str)
Parameter | Description |
---|---|
comp | the component that should display the text |
str | the str |
public static Dimension getStringDimension(Component comp, String str)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2010 BREDEX GmbH. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww .java2s. c o m*/ * BREDEX GmbH - initial API and implementation and/or initial documentation *******************************************************************************/ import java.awt.Component; import java.awt.Dimension; import java.awt.FontMetrics; public class Main { /** * Calculates the dimension of a string * @param comp the component that should display the text * @param str the str * @return the dimension */ public static Dimension getStringDimension(Component comp, String str) { FontMetrics fm = comp.getFontMetrics(comp.getFont()); return new Dimension(fm.stringWidth(str), fm.getHeight()); } }