KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Label


1 /*
2  * @(#)Label.java 1.57 04/03/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.peer.LabelPeer;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import javax.accessibility.*;
13
14 /**
15  * A <code>Label</code> object is a component for placing text in a
16  * container. A label displays a single line of read-only text.
17  * The text can be changed by the application, but a user cannot edit it
18  * directly.
19  * <p>
20  * For example, the code&nbsp;.&nbsp;.&nbsp;.
21  * <p>
22  * <hr><blockquote><pre>
23  * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
24  * add(new Label("Hi There!"));
25  * add(new Label("Another Label"));
26  * </pre></blockquote><hr>
27  * <p>
28  * produces the following labels:
29  * <p>
30  * <img SRC="doc-files/Label-1.gif" alt="Two labels: 'Hi There!' and 'Another label'"
31  * ALIGN=center HSPACE=10 VSPACE=7>
32  *
33  * @version 1.57, 03/16/04
34  * @author Sami Shaio
35  * @since JDK1.0
36  */

37 public class Label extends Component JavaDoc implements Accessible {
38
39     static {
40         /* ensure that the necessary native libraries are loaded */
41     Toolkit.loadLibraries();
42         if (!GraphicsEnvironment.isHeadless()) {
43             initIDs();
44         }
45     }
46
47     /**
48      * Indicates that the label should be left justified.
49      */

50     public static final int LEFT = 0;
51
52     /**
53      * Indicates that the label should be centered.
54      */

55     public static final int CENTER = 1;
56
57     /**
58      * Indicates that the label should be right justified.
59      * @since JDK1.0t.
60      */

61     public static final int RIGHT = 2;
62
63     /**
64      * The text of this label.
65      * This text can be modified by the program
66      * but never by the user.
67      *
68      * @serial
69      * @see #getText()
70      * @see #setText(String)
71      */

72     String JavaDoc text;
73
74     /**
75      * The label's alignment. The default alignment is set
76      * to be left justified.
77      *
78      * @serial
79      * @see #getAlignment()
80      * @see #setAlignment(int)
81      */

82     int alignment = LEFT;
83
84     private static final String JavaDoc base = "label";
85     private static int nameCounter = 0;
86
87     /*
88      * JDK 1.1 serialVersionUID
89      */

90      private static final long serialVersionUID = 3094126758329070636L;
91
92     /**
93      * Constructs an empty label.
94      * The text of the label is the empty string <code>""</code>.
95      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
96      * returns true.
97      * @see java.awt.GraphicsEnvironment#isHeadless
98      */

99     public Label() throws HeadlessException JavaDoc {
100     this("", LEFT);
101     }
102
103     /**
104      * Constructs a new label with the specified string of text,
105      * left justified.
106      * @param text the string that the label presents.
107      * A <code>null</code> value
108      * will be accepted without causing a NullPointerException
109      * to be thrown.
110      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
111      * returns true.
112      * @see java.awt.GraphicsEnvironment#isHeadless
113      */

114     public Label(String JavaDoc text) throws HeadlessException JavaDoc {
115         this(text, LEFT);
116     }
117
118     /**
119      * Constructs a new label that presents the specified string of
120      * text with the specified alignment.
121      * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
122      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
123      * @param text the string that the label presents.
124      * A <code>null</code> value
125      * will be accepted without causing a NullPointerException
126      * to be thrown.
127      * @param alignment the alignment value.
128      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
129      * returns true.
130      * @see java.awt.GraphicsEnvironment#isHeadless
131      */

132     public Label(String JavaDoc text, int alignment) throws HeadlessException JavaDoc {
133         GraphicsEnvironment.checkHeadless();
134     this.text = text;
135     setAlignment(alignment);
136     }
137
138     /**
139      * Read a label from an object input stream.
140      * @exception HeadlessException if
141      * <code>GraphicsEnvironment.isHeadless()</code> returns
142      * <code>true</code>
143      * @serial
144      * @since 1.4
145      * @see java.awt.GraphicsEnvironment#isHeadless
146      */

147     private void readObject(ObjectInputStream JavaDoc s)
148         throws ClassNotFoundException JavaDoc, IOException JavaDoc, HeadlessException JavaDoc {
149         GraphicsEnvironment.checkHeadless();
150         s.defaultReadObject();
151     }
152
153     /**
154      * Construct a name for this component. Called by getName() when the
155      * name is <code>null</code>.
156      */

157     String JavaDoc constructComponentName() {
158         synchronized (getClass()) {
159         return base + nameCounter++;
160     }
161     }
162
163     /**
164      * Creates the peer for this label. The peer allows us to
165      * modify the appearance of the label without changing its
166      * functionality.
167      */

168     public void addNotify() {
169         synchronized (getTreeLock()) {
170         if (peer == null)
171             peer = getToolkit().createLabel(this);
172         super.addNotify();
173     }
174     }
175
176     /**
177      * Gets the current alignment of this label. Possible values are
178      * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
179      * <code>Label.CENTER</code>.
180      * @see java.awt.Label#setAlignment
181      */

182     public int getAlignment() {
183     return alignment;
184     }
185
186     /**
187      * Sets the alignment for this label to the specified alignment.
188      * Possible values are <code>Label.LEFT</code>,
189      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
190      * @param alignment the alignment to be set.
191      * @exception IllegalArgumentException if an improper value for
192      * <code>alignment</code> is given.
193      * @see java.awt.Label#getAlignment
194      */

195     public synchronized void setAlignment(int alignment) {
196     switch (alignment) {
197       case LEFT:
198       case CENTER:
199       case RIGHT:
200         this.alignment = alignment;
201             LabelPeer peer = (LabelPeer)this.peer;
202         if (peer != null) {
203         peer.setAlignment(alignment);
204         }
205         return;
206     }
207     throw new IllegalArgumentException JavaDoc("improper alignment: " + alignment);
208     }
209
210     /**
211      * Gets the text of this label.
212      * @return the text of this label, or <code>null</code> if
213      * the text has been set to <code>null</code>.
214      * @see java.awt.Label#setText
215      */

216     public String JavaDoc getText() {
217     return text;
218     }
219
220     /**
221      * Sets the text for this label to the specified text.
222      * @param text the text that this label displays. If
223      * <code>text</code> is <code>null</code>, it is
224      * treated for display purposes like an empty
225      * string <code>""</code>.
226      * @see java.awt.Label#getText
227      */

228     public void setText(String JavaDoc text) {
229         boolean testvalid = false;
230     synchronized (this) {
231         if (text != this.text && (this.text == null ||
232                       !this.text.equals(text))) {
233             this.text = text;
234         LabelPeer peer = (LabelPeer)this.peer;
235         if (peer != null) {
236             peer.setText(text);
237         }
238         testvalid = true;
239         }
240     }
241
242     // This could change the preferred size of the Component.
243
if (testvalid && valid) {
244         invalidate();
245     }
246     }
247
248     /**
249      * Returns a string representing the state of this <code>Label</code>.
250      * This method is intended to be used only for debugging purposes, and the
251      * content and format of the returned string may vary between
252      * implementations. The returned string may be empty but may not be
253      * <code>null</code>.
254      *
255      * @return the parameter string of this label
256      */

257     protected String JavaDoc paramString() {
258     String JavaDoc str = ",align=";
259     switch (alignment) {
260       case LEFT: str += "left"; break;
261       case CENTER: str += "center"; break;
262       case RIGHT: str += "right"; break;
263     }
264     return super.paramString() + str + ",text=" + text;
265     }
266
267     /**
268      * Initialize JNI field and method IDs
269      */

270     private static native void initIDs();
271
272
273 /////////////////
274
// Accessibility support
275
////////////////
276

277
278     /**
279      * Gets the AccessibleContext associated with this Label.
280      * For labels, the AccessibleContext takes the form of an
281      * AccessibleAWTLabel.
282      * A new AccessibleAWTLabel instance is created if necessary.
283      *
284      * @return an AccessibleAWTLabel that serves as the
285      * AccessibleContext of this Label
286      */

287     public AccessibleContext getAccessibleContext() {
288         if (accessibleContext == null) {
289             accessibleContext = new AccessibleAWTLabel();
290         }
291         return accessibleContext;
292     }
293
294     /**
295      * This class implements accessibility support for the
296      * <code>Label</code> class. It provides an implementation of the
297      * Java Accessibility API appropriate to label user-interface elements.
298      */

299     protected class AccessibleAWTLabel extends AccessibleAWTComponent
300     {
301         /*
302          * JDK 1.3 serialVersionUID
303          */

304         private static final long serialVersionUID = -3568967560160480438L;
305
306     public AccessibleAWTLabel() {
307         super();
308     }
309
310         /**
311          * Get the accessible name of this object.
312          *
313          * @return the localized name of the object -- can be null if this
314          * object does not have a name
315          * @see AccessibleContext#setAccessibleName
316          */

317         public String JavaDoc getAccessibleName() {
318             if (accessibleName != null) {
319                 return accessibleName;
320             } else {
321                 if (getText() == null) {
322                     return super.getAccessibleName();
323                 } else {
324                     return getText();
325                 }
326             }
327         }
328
329         /**
330          * Get the role of this object.
331          *
332          * @return an instance of AccessibleRole describing the role of the object
333          * @see AccessibleRole
334          */

335         public AccessibleRole getAccessibleRole() {
336             return AccessibleRole.LABEL;
337         }
338
339     } // inner class AccessibleAWTLabel
340

341 }
342
Popular Tags