KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JPanel


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

7 package javax.swing;
8
9 import java.awt.*;
10
11 import javax.swing.plaf.*;
12 import javax.accessibility.*;
13
14 import java.io.Serializable JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18
19
20 /**
21  * <code>JPanel</code> is a generic lightweight container.
22  * For examples and task-oriented documentation for JPanel, see
23  * <a
24  href="http://java.sun.com/docs/books/tutorial/uiswing/components/panel.html">How to Use Panels</a>,
25  * a section in <em>The Java Tutorial</em>.
26  * <p>
27  * <strong>Warning:</strong>
28  * Serialized objects of this class will not be compatible with
29  * future Swing releases. The current serialization support is
30  * appropriate for short term storage or RMI between applications running
31  * the same version of Swing. As of 1.4, support for long term storage
32  * of all JavaBeans<sup><font size="-2">TM</font></sup>
33  * has been added to the <code>java.beans</code> package.
34  * Please see {@link java.beans.XMLEncoder}.
35  *
36  * @beaninfo
37  * description: A generic lightweight container.
38  *
39  * @version 1.46 12/19/03
40  * @author Arnaud Weber
41  * @author Steve Wilson
42  */

43 public class JPanel extends JComponent JavaDoc implements Accessible
44 {
45     /**
46      * @see #getUIClassID
47      * @see #readObject
48      */

49     private static final String JavaDoc uiClassID = "PanelUI";
50
51     /**
52      * Creates a new JPanel with the specified layout manager and buffering
53      * strategy.
54      *
55      * @param layout the LayoutManager to use
56      * @param isDoubleBuffered a boolean, true for double-buffering, which
57      * uses additional memory space to achieve fast, flicker-free
58      * updates
59      */

60     public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
61         setLayout(layout);
62         setDoubleBuffered(isDoubleBuffered);
63         setUIProperty("opaque", Boolean.TRUE);
64         updateUI();
65     }
66
67     /**
68      * Create a new buffered JPanel with the specified layout manager
69      *
70      * @param layout the LayoutManager to use
71      */

72     public JPanel(LayoutManager layout) {
73         this(layout, true);
74     }
75
76     /**
77      * Creates a new <code>JPanel</code> with <code>FlowLayout</code>
78      * and the specified buffering strategy.
79      * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code>
80      * will use a double buffer.
81      *
82      * @param isDoubleBuffered a boolean, true for double-buffering, which
83      * uses additional memory space to achieve fast, flicker-free
84      * updates
85      */

86     public JPanel(boolean isDoubleBuffered) {
87         this(new FlowLayout(), isDoubleBuffered);
88     }
89
90     /**
91      * Creates a new <code>JPanel</code> with a double buffer
92      * and a flow layout.
93      */

94     public JPanel() {
95         this(true);
96     }
97
98     /**
99      * Resets the UI property with a value from the current look and feel.
100      *
101      * @see JComponent#updateUI
102      */

103     public void updateUI() {
104         setUI((PanelUI)UIManager.getUI(this));
105     }
106
107     /**
108      * Returns the look and feel (L&F) object that renders this component.
109      *
110      * @return the PanelUI object that renders this component
111      * @since 1.4
112      */

113     public PanelUI getUI() {
114         return (PanelUI)ui;
115     }
116
117
118     /**
119      * Sets the look and feel (L&F) object that renders this component.
120      *
121      * @param ui the PanelUI L&F object
122      * @see UIDefaults#getUI
123      * @since 1.4
124      * @beaninfo
125      * bound: true
126      * hidden: true
127      * attribute: visualUpdate true
128      * description: The UI object that implements the Component's LookAndFeel.
129      */

130     public void setUI(PanelUI ui) {
131         super.setUI(ui);
132     }
133
134     /**
135      * Returns a string that specifies the name of the L&F class
136      * that renders this component.
137      *
138      * @return "PanelUI"
139      * @see JComponent#getUIClassID
140      * @see UIDefaults#getUI
141      * @beaninfo
142      * expert: true
143      * description: A string that specifies the name of the L&F class.
144      */

145     public String JavaDoc getUIClassID() {
146         return uiClassID;
147     }
148
149
150     /**
151      * See readObject() and writeObject() in JComponent for more
152      * information about serialization in Swing.
153      */

154     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
155         s.defaultWriteObject();
156         if (getUIClassID().equals(uiClassID)) {
157             byte count = JComponent.getWriteObjCounter(this);
158             JComponent.setWriteObjCounter(this, --count);
159             if (count == 0 && ui != null) {
160                 ui.installUI(this);
161             }
162         }
163     }
164
165
166     /**
167      * Returns a string representation of this JPanel. This method
168      * is intended to be used only for debugging purposes, and the
169      * content and format of the returned string may vary between
170      * implementations. The returned string may be empty but may not
171      * be <code>null</code>.
172      *
173      * @return a string representation of this JPanel.
174      */

175     protected String JavaDoc paramString() {
176     return super.paramString();
177     }
178
179 /////////////////
180
// Accessibility support
181
////////////////
182

183     /**
184      * Gets the AccessibleContext associated with this JPanel.
185      * For JPanels, the AccessibleContext takes the form of an
186      * AccessibleJPanel.
187      * A new AccessibleJPanel instance is created if necessary.
188      *
189      * @return an AccessibleJPanel that serves as the
190      * AccessibleContext of this JPanel
191      */

192     public AccessibleContext getAccessibleContext() {
193         if (accessibleContext == null) {
194             accessibleContext = new AccessibleJPanel();
195         }
196         return accessibleContext;
197     }
198
199     /**
200      * This class implements accessibility support for the
201      * <code>JPanel</code> class. It provides an implementation of the
202      * Java Accessibility API appropriate to panel user-interface
203      * elements.
204      * <p>
205      * <strong>Warning:</strong>
206      * Serialized objects of this class will not be compatible with
207      * future Swing releases. The current serialization support is
208      * appropriate for short term storage or RMI between applications running
209      * the same version of Swing. As of 1.4, support for long term storage
210      * of all JavaBeans<sup><font size="-2">TM</font></sup>
211      * has been added to the <code>java.beans</code> package.
212      * Please see {@link java.beans.XMLEncoder}.
213      */

214     protected class AccessibleJPanel extends AccessibleJComponent {
215
216         /**
217          * Get the role of this object.
218          *
219          * @return an instance of AccessibleRole describing the role of the
220          * object
221          */

222         public AccessibleRole getAccessibleRole() {
223             return AccessibleRole.PANEL;
224         }
225     }
226 }
227
228
Popular Tags