1 /* 2 * Copyright (c) 1997, 2014, 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://docs.oracle.com/javase/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™ 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 * @since 1.2 65 */ 66 @SuppressWarnings("serial") // Same-version serialization only 67 public class JPanel extends JComponent implements Accessible 68 { 69 /** 70 * @see #getUIClassID 71 * @see #readObject 72 */ 73 private static final String uiClassID = "PanelUI"; 74 75 /** 76 * Creates a new JPanel with the specified layout manager and buffering 77 * strategy. 78 * 79 * @param layout the LayoutManager to use 80 * @param isDoubleBuffered a boolean, true for double-buffering, which 81 * uses additional memory space to achieve fast, flicker-free 82 * updates 83 */ 84 public JPanel(LayoutManager layout, boolean isDoubleBuffered) { 85 setLayout(layout); 86 setDoubleBuffered(isDoubleBuffered); 87 setUIProperty("opaque", Boolean.TRUE); 88 updateUI(); 89 } 90 91 /** 92 * Create a new buffered JPanel with the specified layout manager 93 * 94 * @param layout the LayoutManager to use 95 */ 96 public JPanel(LayoutManager layout) { 97 this(layout, true); 98 } 99 100 /** 101 * Creates a new <code>JPanel</code> with <code>FlowLayout</code> 102 * and the specified buffering strategy. 103 * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code> 104 * will use a double buffer. 105 * 106 * @param isDoubleBuffered a boolean, true for double-buffering, which 107 * uses additional memory space to achieve fast, flicker-free 108 * updates 109 */ 110 public JPanel(boolean isDoubleBuffered) { 111 this(new FlowLayout(), isDoubleBuffered); 112 } 113 114 /** 115 * Creates a new <code>JPanel</code> with a double buffer 116 * and a flow layout. 117 */ 118 public JPanel() { 119 this(true); 120 } 121 122 /** 123 * Resets the UI property with a value from the current look and feel. 124 * 125 * @see JComponent#updateUI 126 */ 127 public void updateUI() { 128 setUI((PanelUI)UIManager.getUI(this)); 129 } 130 131 /** 132 * Returns the look and feel (L&amp;F) object that renders this component. 133 * 134 * @return the PanelUI object that renders this component 135 * @since 1.4 136 */ 137 public PanelUI getUI() { 138 return (PanelUI)ui; 139 } 140 141 142 /** 143 * Sets the look and feel (L&F) object that renders this component. 144 * 145 * @param ui the PanelUI L&F object 146 * @see UIDefaults#getUI 147 * @since 1.4 148 * @beaninfo 149 * bound: true 150 * hidden: true 151 * attribute: visualUpdate true 152 * description: The UI object that implements the Component's LookAndFeel. 153 */ 154 public void setUI(PanelUI ui) { 155 super.setUI(ui); 156 } 157 158 /** 159 * Returns a string that specifies the name of the L&F class 160 * that renders this component. 161 * 162 * @return "PanelUI" 163 * @see JComponent#getUIClassID 164 * @see UIDefaults#getUI 165 * @beaninfo 166 * expert: true 167 * description: A string that specifies the name of the L&F class. 168 */ 169 public String getUIClassID() { 170 return uiClassID; 171 } 172 173 174 /** 175 * See readObject() and writeObject() in JComponent for more 176 * information about serialization in Swing. 177 */ 178 private void writeObject(ObjectOutputStream s) throws IOException { 179 s.defaultWriteObject(); 180 if (getUIClassID().equals(uiClassID)) { 181 byte count = JComponent.getWriteObjCounter(this); 182 JComponent.setWriteObjCounter(this, --count); 183 if (count == 0 && ui != null) { 184 ui.installUI(this); 185 } 186 } 187 } 188 189 190 /** 191 * Returns a string representation of this JPanel. This method 192 * is intended to be used only for debugging purposes, and the 193 * content and format of the returned string may vary between 194 * implementations. The returned string may be empty but may not 195 * be <code>null</code>. 196 * 197 * @return a string representation of this JPanel. 198 */ 199 protected String paramString() { 200 return super.paramString(); 201 } 202 203 ///////////////// 204 // Accessibility support 205 //////////////// 206 207 /** 208 * Gets the AccessibleContext associated with this JPanel. 209 * For JPanels, the AccessibleContext takes the form of an 210 * AccessibleJPanel. 211 * A new AccessibleJPanel instance is created if necessary. 212 * 213 * @return an AccessibleJPanel that serves as the 214 * AccessibleContext of this JPanel 215 */ 216 public AccessibleContext getAccessibleContext() { 217 if (accessibleContext == null) { 218 accessibleContext = new AccessibleJPanel(); 219 } 220 return accessibleContext; 221 } 222 223 /** 224 * This class implements accessibility support for the 225 * <code>JPanel</code> class. It provides an implementation of the 226 * Java Accessibility API appropriate to panel user-interface 227 * elements. 228 * <p> 229 * <strong>Warning:</strong> 230 * Serialized objects of this class will not be compatible with 231 * future Swing releases. The current serialization support is 232 * appropriate for short term storage or RMI between applications running 233 * the same version of Swing. As of 1.4, support for long term storage 234 * of all JavaBeans™ 235 * has been added to the <code>java.beans</code> package. 236 * Please see {@link java.beans.XMLEncoder}. 237 */ 238 @SuppressWarnings("serial") // Same-version serialization only 239 protected class AccessibleJPanel extends AccessibleJComponent { 240 241 /** 242 * Get the role of this object. 243 * 244 * @return an instance of AccessibleRole describing the role of the 245 * object 246 */ 247 public AccessibleRole getAccessibleRole() { 248 return AccessibleRole.PANEL; 249 } 250 } 251 }