Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 BSI Business Systems Integration AG. * 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: * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.awt.Component; import java.awt.Dimension; import javax.swing.JLabel; import javax.swing.text.View; public class Main { public static final int MIN = 0; public static final int PREF = 1; public static final int MAX = 2; public static Dimension getSize(Component c, int sizeflag) { if (c == null) { return new Dimension(0, 0); } //special case due to swing bug: html labels need to know the current parent layout's size if (c instanceof JLabel) { View v = (View) ((JLabel) c).getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); if (v != null) { switch (sizeflag) { case MIN: { Dimension d = new Dimension(c.getPreferredSize()); d.width = 1; return d; } case PREF: { Dimension d = new Dimension(c.getPreferredSize()); return d; } case MAX: { Dimension d = new Dimension(10240, 10240); d.width = 1; return d; } } } } // switch (sizeflag) { case MIN: { return new Dimension(c.getMinimumSize()); } case PREF: { return new Dimension(c.getPreferredSize()); } case MAX: { return new Dimension(c.getMaximumSize()); } } return new Dimension(c.getPreferredSize()); } }