1 /* 2 * Copyright (c) 1997, 2021, 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 26 package javax.swing; 27 28 import java.awt.FlowLayout; 29 import java.awt.LayoutManager; 30 import java.beans.BeanProperty; 31 import java.beans.JavaBean; 32 import java.io.IOException; 33 import java.io.ObjectOutputStream; 34 import java.io.Serial; 35 36 import javax.accessibility.Accessible; 37 import javax.accessibility.AccessibleContext; 38 import javax.accessibility.AccessibleRole; 39 import javax.swing.plaf.PanelUI; 40 41 /** 42 * <code>JPanel</code> is a generic lightweight container. 43 * For examples and task-oriented documentation for JPanel, see 44 * <a 45 href="https://docs.oracle.com/javase/tutorial/uiswing/components/panel.html">How to Use Panels</a>, 46 * a section in <em>The Java Tutorial</em>. 47 * <p> 48 * <strong>Warning:</strong> Swing is not thread safe. For more 49 * information see <a 50 * href="package-summary.html#threading">Swing's Threading 51 * Policy</a>. 52 * <p> 53 * <strong>Warning:</strong> 54 * Serialized objects of this class will not be compatible with 55 * future Swing releases. The current serialization support is 56 * appropriate for short term storage or RMI between applications running 57 * the same version of Swing. As of 1.4, support for long term storage 58 * of all JavaBeans 59 * has been added to the <code>java.beans</code> package. 60 * Please see {@link java.beans.XMLEncoder}. 61 * 62 * @author Arnaud Weber 63 * @author Steve Wilson 64 * @since 1.2 65 */ 66 @JavaBean(defaultProperty = "UI", description = "A generic lightweight container.") 67 @SuppressWarnings("serial") // Same-version serialization only 68 public class JPanel extends JComponent implements Accessible 69 { 70 /** 71 * @see #getUIClassID 72 * @see #readObject 73 */ 74 private static final String uiClassID = "PanelUI"; 75 76 /** 77 * Creates a new JPanel with the specified layout manager and buffering 78 * strategy. 79 * 80 * @param layout the LayoutManager to use 81 * @param isDoubleBuffered a boolean, true for double-buffering, which 82 * uses additional memory space to achieve fast, flicker-free 83 * updates 84 */ JPanel(LayoutManager layout, boolean isDoubleBuffered)85 public JPanel(LayoutManager layout, boolean isDoubleBuffered) { 86 setLayout(layout); 87 setDoubleBuffered(isDoubleBuffered); 88 setUIProperty("opaque", Boolean.TRUE); 89 updateUI(); 90 } 91 92 /** 93 * Create a new buffered JPanel with the specified layout manager 94 * 95 * @param layout the LayoutManager to use 96 */ JPanel(LayoutManager layout)97 public JPanel(LayoutManager layout) { 98 this(layout, true); 99 } 100 101 /** 102 * Creates a new <code>JPanel</code> with <code>FlowLayout</code> 103 * and the specified buffering strategy. 104 * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code> 105 * will use a double buffer. 106 * 107 * @param isDoubleBuffered a boolean, true for double-buffering, which 108 * uses additional memory space to achieve fast, flicker-free 109 * updates 110 */ JPanel(boolean isDoubleBuffered)111 public JPanel(boolean isDoubleBuffered) { 112 this(new FlowLayout(), isDoubleBuffered); 113 } 114 115 /** 116 * Creates a new <code>JPanel</code> with a double buffer 117 * and a flow layout. 118 */ JPanel()119 public JPanel() { 120 this(true); 121 } 122 123 /** 124 * Resets the UI property with a value from the current look and feel. 125 * 126 * @see JComponent#updateUI 127 */ updateUI()128 public void updateUI() { 129 setUI((PanelUI)UIManager.getUI(this)); 130 } 131 132 /** 133 * Returns the look and feel (L&F) object that renders this component. 134 * 135 * @return the PanelUI object that renders this component 136 * @since 1.4 137 */ getUI()138 public PanelUI getUI() { 139 return (PanelUI)ui; 140 } 141 142 143 /** 144 * Sets the look and feel (L&F) object that renders this component. 145 * 146 * @param ui the PanelUI L&F object 147 * @see UIDefaults#getUI 148 * @since 1.4 149 */ 150 @BeanProperty(hidden = true, visualUpdate = true, description 151 = "The UI object that implements the Component's LookAndFeel.") setUI(PanelUI ui)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 the string "PanelUI" 161 * @see JComponent#getUIClassID 162 * @see UIDefaults#getUI 163 */ 164 @BeanProperty(bound = false, expert = true, description 165 = "A string that specifies the name of the L&F class.") getUIClassID()166 public String getUIClassID() { 167 return uiClassID; 168 } 169 170 171 /** 172 * See readObject() and writeObject() in JComponent for more 173 * information about serialization in Swing. 174 */ 175 @Serial writeObject(ObjectOutputStream s)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 */ paramString()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 @BeanProperty(bound = false) getAccessibleContext()215 public AccessibleContext getAccessibleContext() { 216 if (accessibleContext == null) { 217 accessibleContext = new AccessibleJPanel(); 218 } 219 return accessibleContext; 220 } 221 222 /** 223 * This class implements accessibility support for the 224 * <code>JPanel</code> class. It provides an implementation of the 225 * Java Accessibility API appropriate to panel user-interface 226 * elements. 227 * <p> 228 * <strong>Warning:</strong> 229 * Serialized objects of this class will not be compatible with 230 * future Swing releases. The current serialization support is 231 * appropriate for short term storage or RMI between applications running 232 * the same version of Swing. As of 1.4, support for long term storage 233 * of all JavaBeans 234 * has been added to the <code>java.beans</code> package. 235 * Please see {@link java.beans.XMLEncoder}. 236 */ 237 @SuppressWarnings("serial") // Same-version serialization only 238 protected class AccessibleJPanel extends AccessibleJComponent { 239 240 /** 241 * Constructs an {@code AccessibleJPanel}. 242 */ AccessibleJPanel()243 protected AccessibleJPanel() {} 244 245 /** 246 * Get the role of this object. 247 * 248 * @return an instance of AccessibleRole describing the role of the 249 * object 250 */ getAccessibleRole()251 public AccessibleRole getAccessibleRole() { 252 return AccessibleRole.PANEL; 253 } 254 } 255 } 256