001    package de.mospace.swing.text;
002    
003    /* de.mospace.swing library
004     * Copyright (C) 2005 Moritz Ringler
005     * $Id: LabeledJTextField.java 12 2007-07-27 09:48:53Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.awt.Dimension;
023    import java.awt.FlowLayout;
024    import java.awt.event.ActionListener;
025    import javax.swing.InputVerifier;
026    import javax.swing.JLabel;
027    import javax.swing.JPanel;
028    import javax.swing.JTextField;
029    
030    /** A JTextField with a label on the left.
031     *
032     * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
033     * @author Moritz Ringler
034    **/
035    public class LabeledJTextField extends JPanel{
036        private final JTextField jTextField;
037    
038        /** Constructs a new text field with the specified label
039        * and initial text.
040        * @param lbel the label for the text field
041        * @param text the initial text
042        */
043        public LabeledJTextField(String lbel,String text){
044          FlowLayout flowLayout = new FlowLayout();
045          flowLayout.setAlignment(FlowLayout.LEFT);
046          jTextField=new JTextField();
047          jTextField.setMinimumSize(new Dimension(21, 20));
048          jTextField.setPreferredSize(new Dimension(35, 20));
049          jTextField.setText(text);
050          JLabel jLabel = new JLabel();
051          jLabel.setText(lbel);
052          this.setMinimumSize(new Dimension(112, 15));
053          this.setLayout(flowLayout);
054          this.add(jTextField,null);
055          this.add(jLabel,null);
056        }
057    
058        /** Adds an action listener to the text field.
059        * @param al the action listener to add
060        * @see javax.swing.JTextField#addActionListener
061        */
062        public void addActionListener(ActionListener al){
063          jTextField.addActionListener(al);
064        }
065    
066        /** Sets an input verifier for the text field
067        * @param iv the new inout verifier for the text field
068        * @see javax.swing.JTextField#setInputVerifier
069        */
070        public void setInputVerifier(InputVerifier iv){
071          jTextField.setInputVerifier(iv);
072        }
073    
074        /** Returns the current text of this labeled text field.
075        * @return the current text
076        * @see javax.swing.JTextField#getText
077        */
078        public String getText(){
079          return jTextField.getText();
080        }
081    }