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. import java.awt.*;
  9. import javax.swing.plaf.*;
  10. import javax.accessibility.*;
  11. import java.io.Serializable;
  12. import java.io.ObjectOutputStream;
  13. import java.io.ObjectInputStream;
  14. import java.io.IOException;
  15. /**
  16. * <code>JPanel</code> is a generic lightweight container.
  17. * For examples and task-oriented documentation for JPanel, see
  18. * <a
  19. href="http://java.sun.com/docs/books/tutorial/uiswing/components/panel.html">How to Use Panels</a>,
  20. * a section in <em>The Java Tutorial</em>.
  21. * <p>
  22. * <strong>Warning:</strong>
  23. * Serialized objects of this class will not be compatible with
  24. * future Swing releases. The current serialization support is
  25. * appropriate for short term storage or RMI between applications running
  26. * the same version of Swing. As of 1.4, support for long term storage
  27. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  28. * has been added to the <code>java.beans</code> package.
  29. * Please see {@link java.beans.XMLEncoder}.
  30. *
  31. * @beaninfo
  32. * description: A generic lightweight container.
  33. *
  34. * @version 1.46 12/19/03
  35. * @author Arnaud Weber
  36. * @author Steve Wilson
  37. */
  38. public class JPanel extends JComponent implements Accessible
  39. {
  40. /**
  41. * @see #getUIClassID
  42. * @see #readObject
  43. */
  44. private static final String uiClassID = "PanelUI";
  45. /**
  46. * Creates a new JPanel with the specified layout manager and buffering
  47. * strategy.
  48. *
  49. * @param layout the LayoutManager to use
  50. * @param isDoubleBuffered a boolean, true for double-buffering, which
  51. * uses additional memory space to achieve fast, flicker-free
  52. * updates
  53. */
  54. public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
  55. setLayout(layout);
  56. setDoubleBuffered(isDoubleBuffered);
  57. setUIProperty("opaque", Boolean.TRUE);
  58. updateUI();
  59. }
  60. /**
  61. * Create a new buffered JPanel with the specified layout manager
  62. *
  63. * @param layout the LayoutManager to use
  64. */
  65. public JPanel(LayoutManager layout) {
  66. this(layout, true);
  67. }
  68. /**
  69. * Creates a new <code>JPanel</code> with <code>FlowLayout</code>
  70. * and the specified buffering strategy.
  71. * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code>
  72. * will use a double buffer.
  73. *
  74. * @param isDoubleBuffered a boolean, true for double-buffering, which
  75. * uses additional memory space to achieve fast, flicker-free
  76. * updates
  77. */
  78. public JPanel(boolean isDoubleBuffered) {
  79. this(new FlowLayout(), isDoubleBuffered);
  80. }
  81. /**
  82. * Creates a new <code>JPanel</code> with a double buffer
  83. * and a flow layout.
  84. */
  85. public JPanel() {
  86. this(true);
  87. }
  88. /**
  89. * Resets the UI property with a value from the current look and feel.
  90. *
  91. * @see JComponent#updateUI
  92. */
  93. public void updateUI() {
  94. setUI((PanelUI)UIManager.getUI(this));
  95. }
  96. /**
  97. * Returns the look and feel (L&F) object that renders this component.
  98. *
  99. * @return the PanelUI object that renders this component
  100. * @since 1.4
  101. */
  102. public PanelUI getUI() {
  103. return (PanelUI)ui;
  104. }
  105. /**
  106. * Sets the look and feel (L&F) object that renders this component.
  107. *
  108. * @param ui the PanelUI L&F object
  109. * @see UIDefaults#getUI
  110. * @since 1.4
  111. * @beaninfo
  112. * bound: true
  113. * hidden: true
  114. * attribute: visualUpdate true
  115. * description: The UI object that implements the Component's LookAndFeel.
  116. */
  117. public void setUI(PanelUI ui) {
  118. super.setUI(ui);
  119. }
  120. /**
  121. * Returns a string that specifies the name of the L&F class
  122. * that renders this component.
  123. *
  124. * @return "PanelUI"
  125. * @see JComponent#getUIClassID
  126. * @see UIDefaults#getUI
  127. * @beaninfo
  128. * expert: true
  129. * description: A string that specifies the name of the L&F class.
  130. */
  131. public String getUIClassID() {
  132. return uiClassID;
  133. }
  134. /**
  135. * See readObject() and writeObject() in JComponent for more
  136. * information about serialization in Swing.
  137. */
  138. private void writeObject(ObjectOutputStream s) throws IOException {
  139. s.defaultWriteObject();
  140. if (getUIClassID().equals(uiClassID)) {
  141. byte count = JComponent.getWriteObjCounter(this);
  142. JComponent.setWriteObjCounter(this, --count);
  143. if (count == 0 && ui != null) {
  144. ui.installUI(this);
  145. }
  146. }
  147. }
  148. /**
  149. * Returns a string representation of this JPanel. This method
  150. * is intended to be used only for debugging purposes, and the
  151. * content and format of the returned string may vary between
  152. * implementations. The returned string may be empty but may not
  153. * be <code>null</code>.
  154. *
  155. * @return a string representation of this JPanel.
  156. */
  157. protected String paramString() {
  158. return super.paramString();
  159. }
  160. /////////////////
  161. // Accessibility support
  162. ////////////////
  163. /**
  164. * Gets the AccessibleContext associated with this JPanel.
  165. * For JPanels, the AccessibleContext takes the form of an
  166. * AccessibleJPanel.
  167. * A new AccessibleJPanel instance is created if necessary.
  168. *
  169. * @return an AccessibleJPanel that serves as the
  170. * AccessibleContext of this JPanel
  171. */
  172. public AccessibleContext getAccessibleContext() {
  173. if (accessibleContext == null) {
  174. accessibleContext = new AccessibleJPanel();
  175. }
  176. return accessibleContext;
  177. }
  178. /**
  179. * This class implements accessibility support for the
  180. * <code>JPanel</code> class. It provides an implementation of the
  181. * Java Accessibility API appropriate to panel user-interface
  182. * elements.
  183. * <p>
  184. * <strong>Warning:</strong>
  185. * Serialized objects of this class will not be compatible with
  186. * future Swing releases. The current serialization support is
  187. * appropriate for short term storage or RMI between applications running
  188. * the same version of Swing. As of 1.4, support for long term storage
  189. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  190. * has been added to the <code>java.beans</code> package.
  191. * Please see {@link java.beans.XMLEncoder}.
  192. */
  193. protected class AccessibleJPanel extends AccessibleJComponent {
  194. /**
  195. * Get the role of this object.
  196. *
  197. * @return an instance of AccessibleRole describing the role of the
  198. * object
  199. */
  200. public AccessibleRole getAccessibleRole() {
  201. return AccessibleRole.PANEL;
  202. }
  203. }
  204. }