KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > gui > JLabeledTextField


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/gui/JLabeledTextField.java,v 1.5 2004/02/11 23:48:32 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.FocusEvent JavaDoc;
23 import java.awt.event.FocusListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33
34 /**
35  * A Helper component that wraps a JTextField with a label into
36  * a JPanel (this). This component also has an efficient event handling
37  * mechanism for handling the text changing in the Text Field. The registered
38  * change listeners are only called when the text has changed.
39  *
40  * @author S.Coleman
41  * @version $Revision: 1.5 $
42  */

43 public class JLabeledTextField
44     extends JPanel JavaDoc
45     implements JLabeledField, FocusListener JavaDoc
46 {
47     private JLabel JavaDoc mLabel;
48     private JTextField JavaDoc mTextField;
49
50     // Maybe move to vector if MT problems occur
51
private ArrayList JavaDoc mChangeListeners = new ArrayList JavaDoc(3);
52
53     // A temporary cache for the focus listener
54
private String JavaDoc oldValue = "";
55
56     /**
57      * Default constructor, The label and the Text field are left empty.
58      */

59     public JLabeledTextField()
60     {
61         this("", 20);
62     }
63
64     /**
65      * Constructs a new component with the label displaying the
66      * passed text.
67      *
68      * @param pLabel The text to in the label.
69      */

70     public JLabeledTextField(String JavaDoc pLabel)
71     {
72         this(pLabel, 20);
73     }
74
75     public JLabeledTextField(String JavaDoc pLabel, int size)
76     {
77         super();
78         mTextField = createTextField(size);
79         mLabel = new JLabel JavaDoc(pLabel);
80         mLabel.setLabelFor(mTextField);
81         init();
82     }
83
84     public List JavaDoc getComponentList()
85     {
86         List JavaDoc comps = new LinkedList JavaDoc();
87         comps.add(mLabel);
88         comps.add(mTextField);
89         return comps;
90     }
91
92     public void setEnabled(boolean enable)
93     {
94         super.setEnabled(enable);
95         mTextField.setEnabled(enable);
96     }
97
98     protected JTextField JavaDoc createTextField(int size)
99     {
100         return new JTextField JavaDoc(size);
101     }
102     
103     /**
104      * Initialises all of the components on this panel.
105      */

106     private void init()
107     {
108         setLayout(new BorderLayout JavaDoc(5, 0));
109         // Register the handler for focus listening. This handler will
110
// only notify the registered when the text changes from when
111
// the focus is gained to when it is lost.
112
mTextField.addFocusListener(this);
113
114         // Add the sub components
115
add(mLabel, BorderLayout.WEST);
116         add(mTextField, BorderLayout.CENTER);
117     }
118
119     /**
120      * Callback method when the focus to the Text Field component
121      * is lost.
122      *
123      * @param pFocusEvent The focus event that occured.
124      */

125     public void focusLost(FocusEvent JavaDoc pFocusEvent)
126     {
127         // Compare if the value has changed, since we received focus.
128
if (!oldValue.equals(mTextField.getText()))
129         {
130             notifyChangeListeners();
131         }
132     }
133
134     /**
135      * Catch what the value was when focus was gained.
136      */

137     public void focusGained(FocusEvent JavaDoc pFocusEvent)
138     {
139         oldValue = mTextField.getText();
140     }
141
142     /**
143      * Set the text displayed in the label.
144      *
145      * @param pLabel The new label text.
146      */

147     public void setLabel(String JavaDoc pLabel)
148     {
149         mLabel.setText(pLabel);
150     }
151
152     /**
153      * Set the text displayed in the Text Field.
154      *
155      * @param pText The new text to display in the text field.
156      */

157     public void setText(String JavaDoc pText)
158     {
159         mTextField.setText(pText);
160     }
161
162     /**
163      * Returns the text in the Text Field.
164      *
165      * @return The text in the Text Field.
166      */

167     public String JavaDoc getText()
168     {
169         return mTextField.getText();
170     }
171
172     /**
173      * Returns the text of the label.
174      *
175      * @return The text of the label.
176      */

177     public String JavaDoc getLabel()
178     {
179         return mLabel.getText();
180     }
181
182     /**
183      * Adds a change listener, that will be notified when the text in the
184      * text field is changed. The ChangeEvent that will be passed
185      * to registered listeners will contain this object as the source, allowing
186      * the new text to be extracted using the {@link #getText() getText} method.
187      *
188      * @param pChangeListener The listener to add
189      */

190     public void addChangeListener(ChangeListener JavaDoc pChangeListener)
191     {
192         mChangeListeners.add(pChangeListener);
193     }
194
195     /**
196      * Removes a change listener.
197      *
198      * @param pChangeListener The change listener to remove.
199      */

200     public void removeChangeListener(ChangeListener JavaDoc pChangeListener)
201     {
202         mChangeListeners.remove(pChangeListener);
203     }
204
205     /**
206      * Notify all registered change listeners that the
207      * text in the text field has changed.
208      */

209     protected void notifyChangeListeners()
210     {
211         ChangeEvent JavaDoc ce = new ChangeEvent JavaDoc(this);
212         for (int index = 0; index < mChangeListeners.size(); index++)
213         {
214             ((ChangeListener JavaDoc) mChangeListeners.get(index)).stateChanged(ce);
215         }
216     }
217 }
218
Popular Tags