1. /*
  2. * @(#)JPanel.java 1.31 01/11/29
  3. *
  4. * Copyright 2002 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. * JPanel is a generic lightweight container.
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @see <a href="http://java.sun.com/products/jfc/swingdoc-archive/mixing.html">
  26. * Mixing Heavy and Light Components</a>
  27. *
  28. * @beaninfo
  29. * description: A generic lightweight container.
  30. *
  31. * @version 1.31 11/29/01
  32. * @author Arnaud Weber
  33. * @author Steve Wilson
  34. */
  35. public class JPanel extends JComponent implements Accessible
  36. {
  37. /**
  38. * @see #getUIClassID
  39. * @see #readObject
  40. */
  41. private static final String uiClassID = "PanelUI";
  42. private static final FlowLayout defaultLayout = new FlowLayout();
  43. /**
  44. * Creates a new JPanel with the specified layout manager and buffering
  45. * strategy.
  46. *
  47. * @param layout the LayoutManager to use
  48. * @param isDoubleBuffered a boolean, true for double-buffering, which
  49. * uses additional memory space to achieve fast, flicker-free
  50. * updates
  51. */
  52. public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
  53. setLayout(layout);
  54. setDoubleBuffered(isDoubleBuffered);
  55. setOpaque(true);
  56. updateUI();
  57. }
  58. /**
  59. * Create a new buffered JPanel with the specified layout manager
  60. *
  61. * @param layout the LayoutManager to use
  62. */
  63. public JPanel(LayoutManager layout) {
  64. this(layout, true);
  65. }
  66. /**
  67. * Create a new JPanel with FlowLayout and the specified buffering
  68. * strategy. If <code>isDoubleBuffered</code> is true, the JPanel
  69. * will use a double buffer.
  70. *
  71. * @param layout the LayoutManager to use
  72. * @param isDoubleBuffered a boolean, true for double-buffering, which
  73. * uses additional memory space to achieve fast, flicker-free
  74. * updates
  75. */
  76. public JPanel(boolean isDoubleBuffered) {
  77. this(defaultLayout, isDoubleBuffered);
  78. }
  79. /**
  80. * Create a new JPanel with a double buffer and a flow layout
  81. */
  82. public JPanel() {
  83. this(defaultLayout, true);
  84. }
  85. /**
  86. * Notification from the UIFactory that the L&F
  87. * has changed.
  88. *
  89. * @see JComponent#updateUI
  90. */
  91. public void updateUI() {
  92. setUI((PanelUI)UIManager.getUI(this));
  93. }
  94. /**
  95. * Returns a string that specifies the name of the L&F class
  96. * that renders this component.
  97. *
  98. * @return "PanelUI"
  99. * @see JComponent#getUIClassID
  100. * @see UIDefaults#getUI
  101. * @beaninfo
  102. * expert: true
  103. * description: A string that specifies the name of the L&F class.
  104. */
  105. public String getUIClassID() {
  106. return uiClassID;
  107. }
  108. /**
  109. * See readObject() and writeObject() in JComponent for more
  110. * information about serialization in Swing.
  111. */
  112. private void writeObject(ObjectOutputStream s) throws IOException {
  113. s.defaultWriteObject();
  114. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  115. ui.installUI(this);
  116. }
  117. }
  118. /**
  119. * Returns a string representation of this JPanel. This method
  120. * is intended to be used only for debugging purposes, and the
  121. * content and format of the returned string may vary between
  122. * implementations. The returned string may be empty but may not
  123. * be <code>null</code>.
  124. *
  125. * @return a string representation of this JPanel.
  126. */
  127. protected String paramString() {
  128. String defaultLayoutString = (defaultLayout != null ?
  129. defaultLayout.toString() : "");
  130. return super.paramString() +
  131. ",defaultLayout=" + defaultLayoutString;
  132. }
  133. /////////////////
  134. // Accessibility support
  135. ////////////////
  136. /**
  137. * Get the AccessibleContext associated with this JComponent
  138. *
  139. * @return the AccessibleContext of this JComponent
  140. */
  141. public AccessibleContext getAccessibleContext() {
  142. if (accessibleContext == null) {
  143. accessibleContext = new AccessibleJPanel();
  144. }
  145. return accessibleContext;
  146. }
  147. /**
  148. * The class used to obtain the accessible role for this object.
  149. * <p>
  150. * <strong>Warning:</strong>
  151. * Serialized objects of this class will not be compatible with
  152. * future Swing releases. The current serialization support is appropriate
  153. * for short term storage or RMI between applications running the same
  154. * version of Swing. A future release of Swing will provide support for
  155. * long term persistence.
  156. */
  157. protected class AccessibleJPanel extends AccessibleJComponent {
  158. /**
  159. * Get the role of this object.
  160. *
  161. * @return an instance of AccessibleRole describing the role of the
  162. * object
  163. */
  164. public AccessibleRole getAccessibleRole() {
  165. return AccessibleRole.PANEL;
  166. }
  167. }
  168. }