001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.*;
028
029 import javax.swing.plaf.*;
030 import javax.accessibility.*;
031
032 import java.io.Serializable;
033 import java.io.ObjectOutputStream;
034 import java.io.ObjectInputStream;
035 import java.io.IOException;
036
037 /**
038 * <code>JPanel</code> is a generic lightweight container.
039 * For examples and task-oriented documentation for JPanel, see
040 * <a
041 href="http://java.sun.com/docs/books/tutorial/uiswing/components/panel.html">How to Use Panels</a>,
042 * a section in <em>The Java Tutorial</em>.
043 * <p>
044 * <strong>Warning:</strong> Swing is not thread safe. For more
045 * information see <a
046 * href="package-summary.html#threading">Swing's Threading
047 * Policy</a>.
048 * <p>
049 * <strong>Warning:</strong>
050 * Serialized objects of this class will not be compatible with
051 * future Swing releases. The current serialization support is
052 * appropriate for short term storage or RMI between applications running
053 * the same version of Swing. As of 1.4, support for long term storage
054 * of all JavaBeans<sup><font size="-2">TM</font></sup>
055 * has been added to the <code>java.beans</code> package.
056 * Please see {@link java.beans.XMLEncoder}.
057 *
058 * @beaninfo
059 * description: A generic lightweight container.
060 *
061 * @version 1.54 05/05/07
062 * @author Arnaud Weber
063 * @author Steve Wilson
064 */
065 public class JPanel extends JComponent implements Accessible {
066 /**
067 * @see #getUIClassID
068 * @see #readObject
069 */
070 private static final String uiClassID = "PanelUI";
071
072 /**
073 * Creates a new JPanel with the specified layout manager and buffering
074 * strategy.
075 *
076 * @param layout the LayoutManager to use
077 * @param isDoubleBuffered a boolean, true for double-buffering, which
078 * uses additional memory space to achieve fast, flicker-free
079 * updates
080 */
081 public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
082 setLayout(layout);
083 setDoubleBuffered(isDoubleBuffered);
084 setUIProperty("opaque", Boolean.TRUE);
085 updateUI();
086 }
087
088 /**
089 * Create a new buffered JPanel with the specified layout manager
090 *
091 * @param layout the LayoutManager to use
092 */
093 public JPanel(LayoutManager layout) {
094 this (layout, true);
095 }
096
097 /**
098 * Creates a new <code>JPanel</code> with <code>FlowLayout</code>
099 * and the specified buffering strategy.
100 * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code>
101 * will use a double buffer.
102 *
103 * @param isDoubleBuffered a boolean, true for double-buffering, which
104 * uses additional memory space to achieve fast, flicker-free
105 * updates
106 */
107 public JPanel(boolean isDoubleBuffered) {
108 this (new FlowLayout(), isDoubleBuffered);
109 }
110
111 /**
112 * Creates a new <code>JPanel</code> with a double buffer
113 * and a flow layout.
114 */
115 public JPanel() {
116 this (true);
117 }
118
119 /**
120 * Resets the UI property with a value from the current look and feel.
121 *
122 * @see JComponent#updateUI
123 */
124 public void updateUI() {
125 setUI((PanelUI) UIManager.getUI(this ));
126 }
127
128 /**
129 * Returns the look and feel (L&F) object that renders this component.
130 *
131 * @return the PanelUI object that renders this component
132 * @since 1.4
133 */
134 public PanelUI getUI() {
135 return (PanelUI) ui;
136 }
137
138 /**
139 * Sets the look and feel (L&F) object that renders this component.
140 *
141 * @param ui the PanelUI L&F object
142 * @see UIDefaults#getUI
143 * @since 1.4
144 * @beaninfo
145 * bound: true
146 * hidden: true
147 * attribute: visualUpdate true
148 * description: The UI object that implements the Component's LookAndFeel.
149 */
150 public void setUI(PanelUI ui) {
151 super .setUI(ui);
152 }
153
154 /**
155 * Returns a string that specifies the name of the L&F class
156 * that renders this component.
157 *
158 * @return "PanelUI"
159 * @see JComponent#getUIClassID
160 * @see UIDefaults#getUI
161 * @beaninfo
162 * expert: true
163 * description: A string that specifies the name of the L&F class.
164 */
165 public String getUIClassID() {
166 return uiClassID;
167 }
168
169 /**
170 * See readObject() and writeObject() in JComponent for more
171 * information about serialization in Swing.
172 */
173 private void writeObject(ObjectOutputStream s) throws IOException {
174 s.defaultWriteObject();
175 if (getUIClassID().equals(uiClassID)) {
176 byte count = JComponent.getWriteObjCounter(this );
177 JComponent.setWriteObjCounter(this , --count);
178 if (count == 0 && ui != null) {
179 ui.installUI(this );
180 }
181 }
182 }
183
184 /**
185 * Returns a string representation of this JPanel. This method
186 * is intended to be used only for debugging purposes, and the
187 * content and format of the returned string may vary between
188 * implementations. The returned string may be empty but may not
189 * be <code>null</code>.
190 *
191 * @return a string representation of this JPanel.
192 */
193 protected String paramString() {
194 return super .paramString();
195 }
196
197 /////////////////
198 // Accessibility support
199 ////////////////
200
201 /**
202 * Gets the AccessibleContext associated with this JPanel.
203 * For JPanels, the AccessibleContext takes the form of an
204 * AccessibleJPanel.
205 * A new AccessibleJPanel instance is created if necessary.
206 *
207 * @return an AccessibleJPanel that serves as the
208 * AccessibleContext of this JPanel
209 */
210 public AccessibleContext getAccessibleContext() {
211 if (accessibleContext == null) {
212 accessibleContext = new AccessibleJPanel();
213 }
214 return accessibleContext;
215 }
216
217 /**
218 * This class implements accessibility support for the
219 * <code>JPanel</code> class. It provides an implementation of the
220 * Java Accessibility API appropriate to panel user-interface
221 * elements.
222 * <p>
223 * <strong>Warning:</strong>
224 * Serialized objects of this class will not be compatible with
225 * future Swing releases. The current serialization support is
226 * appropriate for short term storage or RMI between applications running
227 * the same version of Swing. As of 1.4, support for long term storage
228 * of all JavaBeans<sup><font size="-2">TM</font></sup>
229 * has been added to the <code>java.beans</code> package.
230 * Please see {@link java.beans.XMLEncoder}.
231 */
232 protected class AccessibleJPanel extends AccessibleJComponent {
233
234 /**
235 * Get the role of this object.
236 *
237 * @return an instance of AccessibleRole describing the role of the
238 * object
239 */
240 public AccessibleRole getAccessibleRole() {
241 return AccessibleRole.PANEL;
242 }
243 }
244 }
|