1 /* 2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.awt.*; 28 29 import javax.swing.plaf.*; 30 import javax.accessibility.*; 31 32 import java.io.Serializable; 33 import java.io.ObjectOutputStream; 34 import java.io.ObjectInputStream; 35 import java.io.IOException; 36 37 38 /** 39 * <code>JPanel</code> is a generic lightweight container. 40 * For examples and task-oriented documentation for JPanel, see 41 * <a 42 href="http://java.sun.com/docs/books/tutorial/uiswing/components/panel.html">How to Use Panels</a>, 43 * a section in <em>The Java Tutorial</em>. 44 * <p> 45 * <strong>Warning:</strong> Swing is not thread safe. For more 46 * information see <a 47 * href="package-summary.html#threading">Swing's Threading 48 * Policy</a>. 49 * <p> 50 * <strong>Warning:</strong> 51 * Serialized objects of this class will not be compatible with 52 * future Swing releases. The current serialization support is 53 * appropriate for short term storage or RMI between applications running 54 * the same version of Swing. As of 1.4, support for long term storage 55 * of all JavaBeans<sup><font size="-2">TM</font></sup> 56 * has been added to the <code>java.beans</code> package. 57 * Please see {@link java.beans.XMLEncoder}. 58 * 59 * @beaninfo 60 * description: A generic lightweight container. 61 * 62 * @author Arnaud Weber 63 * @author Steve Wilson 64 */ 65 public class JPanel extends JComponent implements Accessible 66 { 67 /** 68 * @see #getUIClassID 69 * @see #readObject 70 */ 71 private static final String uiClassID = "PanelUI"; 72 73 /** 74 * Creates a new JPanel with the specified layout manager and buffering 75 * strategy. 76 * 77 * @param layout the LayoutManager to use 78 * @param isDoubleBuffered a boolean, true for double-buffering, which 79 * uses additional memory space to achieve fast, flicker-free 80 * updates 81 */ 82 public JPanel(LayoutManager layout, boolean isDoubleBuffered) { 83 setLayout(layout); 84 setDoubleBuffered(isDoubleBuffered); 85 setUIProperty("opaque", Boolean.TRUE); 86 updateUI(); 87 } 88 89 /** 90 * Create a new buffered JPanel with the specified layout manager 91 * 92 * @param layout the LayoutManager to use 93 */ 94 public JPanel(LayoutManager layout) { 95 this(layout, true); 96 } 97 98 /** 99 * Creates a new <code>JPanel</code> with <code>FlowLayout</code> 100 * and the specified buffering strategy. 101 * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code> 102 * will use a double buffer. 103 * 104 * @param isDoubleBuffered a boolean, true for double-buffering, which 105 * uses additional memory space to achieve fast, flicker-free 106 * updates 107 */ 108 public JPanel(boolean isDoubleBuffered) { 109 this(new FlowLayout(), isDoubleBuffered); 110 } 111 112 /** 113 * Creates a new <code>JPanel</code> with a double buffer 114 * and a flow layout. 115 */ 116 public JPanel() { 117 this(true); 118 } 119 120 /** 121 * Resets the UI property with a value from the current look and feel. 122 * 123 * @see JComponent#updateUI 124 */ 125 public void updateUI() { 126 setUI((PanelUI)UIManager.getUI(this)); 127 } 128 129 /** 130 * Returns the look and feel (L&amp;F) object that renders this component. 131 * 132 * @return the PanelUI object that renders this component 133 * @since 1.4 134 */ 135 public PanelUI getUI() { 136 return (PanelUI)ui; 137 } 138 139 140 /** 141 * Sets the look and feel (L&F) object that renders this component. 142 * 143 * @param ui the PanelUI L&F object 144 * @see UIDefaults#getUI 145 * @since 1.4 146 * @beaninfo 147 * bound: true 148 * hidden: true 149 * attribute: visualUpdate true 150 * description: The UI object that implements the Component's LookAndFeel. 151 */ 152 public void setUI(PanelUI ui) { 153 super.setUI(ui); 154 } 155 156 /** 157 * Returns a string that specifies the name of the L&F class 158 * that renders this component. 159 * 160 * @return "PanelUI" 161 * @see JComponent#getUIClassID 162 * @see UIDefaults#getUI 163 * @beaninfo 164 * expert: true 165 * description: A string that specifies the name of the L&F class. 166 */ 167 public String getUIClassID() { 168 return uiClassID; 169 } 170 171 172 /** 173 * See readObject() and writeObject() in JComponent for more 174 * information about serialization in Swing. 175 */ 176 private void writeObject(ObjectOutputStream s) throws IOException { 177 s.defaultWriteObject(); 178 if (getUIClassID().equals(uiClassID)) { 179 byte count = JComponent.getWriteObjCounter(this); 180 JComponent.setWriteObjCounter(this, --count); 181 if (count == 0 && ui != null) { 182 ui.installUI(this); 183 } 184 } 185 } 186 187 188 /** 189 * Returns a string representation of this JPanel. This method 190 * is intended to be used only for debugging purposes, and the 191 * content and format of the returned string may vary between 192 * implementations. The returned string may be empty but may not 193 * be <code>null</code>. 194 * 195 * @return a string representation of this JPanel. 196 */ 197 protected String paramString() { 198 return super.paramString(); 199 } 200 201 ///////////////// 202 // Accessibility support 203 //////////////// 204 205 /** 206 * Gets the AccessibleContext associated with this JPanel. 207 * For JPanels, the AccessibleContext takes the form of an 208 * AccessibleJPanel. 209 * A new AccessibleJPanel instance is created if necessary. 210 * 211 * @return an AccessibleJPanel that serves as the 212 * AccessibleContext of this JPanel 213 */ 214 public AccessibleContext getAccessibleContext() { 215 if (accessibleContext == null) { 216 accessibleContext = new AccessibleJPanel(); 217 } 218 return accessibleContext; 219 } 220 221 /** 222 * This class implements accessibility support for the 223 * <code>JPanel</code> class. It provides an implementation of the 224 * Java Accessibility API appropriate to panel user-interface 225 * elements. 226 * <p> 227 * <strong>Warning:</strong> 228 * Serialized objects of this class will not be compatible with 229 * future Swing releases. The current serialization support is 230 * appropriate for short term storage or RMI between applications running 231 * the same version of Swing. As of 1.4, support for long term storage 232 * of all JavaBeans<sup><font size="-2">TM</font></sup> 233 * has been added to the <code>java.beans</code> package. 234 * Please see {@link java.beans.XMLEncoder}. 235 */ 236 protected class AccessibleJPanel extends AccessibleJComponent { 237 238 /** 239 * Get the role of this object. 240 * 241 * @return an instance of AccessibleRole describing the role of the 242 * object 243 */ 244 public AccessibleRole getAccessibleRole() { 245 return AccessibleRole.PANEL; 246 } 247 } 248 }