Java examples for java.awt:Component
set Swing Component Dimension
/***************************************************************************************** * * This file is part of PlanckDB.//w ww. j a v a2 s . c o m * * PlanckDB is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * PlanckDB 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with PlanckDB. If not, see <http://www.gnu.org/licenses/>. * * Copyright (C) Gidon Shabat, 2008 * * Authors: Gidon Shabat <gidi@planckdb.com> * Date: 06/16/2011 * *******************************************************************************************/ import javax.swing.*; import java.awt.*; public class Main{ public static void setDimension(final Component component, final String text) { setDimension(component, getDimension(component, text)); } public static void setDimension(Component component, Dimension dimension) { component.setSize(dimension); component.setPreferredSize(dimension); component.setMaximumSize(dimension); component.setMinimumSize(dimension); } public static Dimension getDimension(final Component component, String text) { FontMetrics fm = component.getFontMetrics(component.getFont()); if (text == null) { text = ""; } if (component instanceof PDBButton) { Icon icon = ((PDBButton) component).getIcon(); int iconWidth = icon == null ? 0 : icon.getIconWidth(); return new Dimension(fm.stringWidth(text) + 40 + iconWidth, Math.max(20, fm.getHeight())); } else if (component instanceof PDBCheckBox) { return new Dimension(fm.stringWidth(text) + 40, 20); } else { return new Dimension(fm.stringWidth(text), fm.getHeight()); } } public static Dimension getDimension(final Component component, final String text, final int x, final int y) { FontMetrics fm = component.getFontMetrics(component.getFont()); return new Dimension(fm.stringWidth(text) + x, fm.getHeight() + y); } }