Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/LabeledComponent.java
40948 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.*;2829import javax.swing.*;3031@SuppressWarnings("serial")32public class LabeledComponent extends JPanel {33JPanel rightPanel;34String labelStr, valueLabelStr, compoundStr;35JLabel label;36JComponent comp;3738public LabeledComponent(String text, JComponent comp) {39this(text, 0, comp);40}4142public LabeledComponent(String text, int mnemonic, JComponent comp) {43super(new BorderLayout(6, 6));4445this.labelStr = text;46this.label = new JLabel(text, JLabel.RIGHT);47this.comp = comp;4849label.setLabelFor(comp);50if (mnemonic > 0) {51label.setDisplayedMnemonic(mnemonic);52}5354add(label, BorderLayout.WEST);55add(comp, BorderLayout.CENTER);56}5758public void setLabel(String str) {59this.labelStr = str;60updateLabel();61}6263public void setValueLabel(String str) {64this.valueLabelStr = str;65updateLabel();66}6768private void updateLabel() {69String str = labelStr;70label.setText(str);71this.compoundStr = str;72JComponent container = (JComponent)getParent();73LabeledComponent.layout(container);74}7576public static void layout(Container container) {77int wMax = 0;7879for (Component c : container.getComponents()) {80if (c instanceof LabeledComponent) {81LabeledComponent lc = (LabeledComponent)c;82lc.label.setPreferredSize(null);83// int w = lc.label.getMinimumSize().width;84int w = lc.label.getPreferredSize().width;85if (w > wMax) {86wMax = w;87}88}89}9091for (Component c : container.getComponents()) {92if (c instanceof LabeledComponent) {93LabeledComponent lc = (LabeledComponent)c;94JLabel label = lc.label;95int h = label.getPreferredSize().height;9697label.setPreferredSize(new Dimension(wMax, h));98label.setHorizontalAlignment(JLabel.RIGHT);99}100}101}102}103104105