Java tutorial
package de.fhg.iais.asc.ui.components.panel; /****************************************************************************** * Copyright 2011 (c) Fraunhofer IAIS Netmedia http://www.iais.fraunhofer.de * * ************************************************************************** * * Licensed under the Apache License, Version 2.0 (the "License"); you may * * not use this file except in compliance with the License. * * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * ******************************************************************************/ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; import org.apache.commons.lang.StringUtils; import de.fhg.iais.asc.ui.i18n.Messages; import de.fhg.iais.commons.constants.Serial; public class LeftLabeledElementJPanel extends JPanel { private static final long serialVersionUID = Serial.GLOBAL_DEFAULT_SERIAL_VERSION_UID; private static final double WEIGHTX_LABEL = 0.1; private static final double WEIGHTX_ELEMENT = 0.3; private final String prefix; private final GridBagConstraints cLabel = new GridBagConstraints(); private final GridBagConstraints cElement = new GridBagConstraints(); private JLabel lastLabel; public LeftLabeledElementJPanel(String prefix) { super(new GridBagLayout()); this.prefix = (StringUtils.isEmpty(prefix)) ? "" : (prefix + "."); this.cLabel.gridx = 0; this.cLabel.gridy = 0; this.cLabel.weightx = WEIGHTX_LABEL; this.cLabel.fill = GridBagConstraints.HORIZONTAL; this.cElement.gridx = 1; this.cElement.gridwidth = 2; this.cElement.gridy = 0; this.cElement.weightx = WEIGHTX_ELEMENT; this.cElement.fill = GridBagConstraints.HORIZONTAL; } public <E extends JComponent> E add(String labelName, E element) { String labelText = Messages.getString(this.prefix + "Label." + labelName); String tooltipText = Messages.getString(this.prefix + "Tooltip." + labelName, null); JLabel label = new JLabel(labelText + " "); label.setToolTipText(tooltipText); this.add(label, this.cLabel); ++this.cLabel.gridy; element.setToolTipText(tooltipText); this.add(element, this.cElement); ++this.cElement.gridy; this.lastLabel = label; return element; } public <E extends JComponent> E add(String labelName, E element, JButton button) { this.cElement.gridx = 2; this.cElement.gridwidth = 1; this.cElement.weightx = WEIGHTX_LABEL; this.add(button, this.cElement); this.cElement.gridx = 1; this.cElement.weightx = WEIGHTX_ELEMENT - WEIGHTX_LABEL; this.add(labelName, element); this.cElement.gridwidth = 2; this.cElement.weightx = WEIGHTX_ELEMENT; return element; } public JLabel getLastLabel() { return this.lastLabel; } }