1. /*
  2. * @(#)JPanel.java 1.37 00/04/06
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.*;
  12. import javax.swing.plaf.*;
  13. import javax.accessibility.*;
  14. import java.io.Serializable;
  15. import java.io.ObjectOutputStream;
  16. import java.io.ObjectInputStream;
  17. import java.io.IOException;
  18. /**
  19. * JPanel is a generic lightweight container.
  20. * For examples and task-oriented documentation for JPanel, see
  21. * <a
  22. href="http://java.sun.com/docs/books/tutorial/uiswing/components/panel.html">How to Use Panels</a>,
  23. * a section in <em>The Java Tutorial</em>.
  24. * <p>
  25. * <strong>Warning:</strong>
  26. * Serialized objects of this class will not be compatible with
  27. * future Swing releases. The current serialization support is appropriate
  28. * for short term storage or RMI between applications running the same
  29. * version of Swing. A future release of Swing will provide support for
  30. * long term persistence.
  31. *
  32. * @beaninfo
  33. * description: A generic lightweight container.
  34. *
  35. * @version 1.37 04/06/00
  36. * @author Arnaud Weber
  37. * @author Steve Wilson
  38. */
  39. public class JPanel extends JComponent implements Accessible
  40. {
  41. /**
  42. * @see #getUIClassID
  43. * @see #readObject
  44. */
  45. private static final String uiClassID = "PanelUI";
  46. private static final FlowLayout defaultLayout = new FlowLayout();
  47. /**
  48. * Creates a new JPanel with the specified layout manager and buffering
  49. * strategy.
  50. *
  51. * @param layout the LayoutManager to use
  52. * @param isDoubleBuffered a boolean, true for double-buffering, which
  53. * uses additional memory space to achieve fast, flicker-free
  54. * updates
  55. */
  56. public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
  57. setLayout(layout);
  58. setDoubleBuffered(isDoubleBuffered);
  59. setOpaque(true);
  60. updateUI();
  61. }
  62. /**
  63. * Create a new buffered JPanel with the specified layout manager
  64. *
  65. * @param layout the LayoutManager to use
  66. */
  67. public JPanel(LayoutManager layout) {
  68. this(layout, true);
  69. }
  70. /**
  71. * Create a new JPanel with FlowLayout and the specified buffering
  72. * strategy. If <code>isDoubleBuffered</code> is true, the JPanel
  73. * will use a double buffer.
  74. *
  75. * @param layout the LayoutManager to use
  76. * @param isDoubleBuffered a boolean, true for double-buffering, which
  77. * uses additional memory space to achieve fast, flicker-free
  78. * updates
  79. */
  80. public JPanel(boolean isDoubleBuffered) {
  81. this(defaultLayout, isDoubleBuffered);
  82. }
  83. /**
  84. * Create a new JPanel with a double buffer and a flow layout
  85. */
  86. public JPanel() {
  87. this(defaultLayout, true);
  88. }
  89. /**
  90. * Notification from the UIFactory that the L&F
  91. * has changed.
  92. *
  93. * @see JComponent#updateUI
  94. */
  95. public void updateUI() {
  96. setUI((PanelUI)UIManager.getUI(this));
  97. }
  98. /**
  99. * Returns a string that specifies the name of the L&F class
  100. * that renders this component.
  101. *
  102. * @return "PanelUI"
  103. * @see JComponent#getUIClassID
  104. * @see UIDefaults#getUI
  105. * @beaninfo
  106. * expert: true
  107. * description: A string that specifies the name of the L&F class.
  108. */
  109. public String getUIClassID() {
  110. return uiClassID;
  111. }
  112. /**
  113. * See readObject() and writeObject() in JComponent for more
  114. * information about serialization in Swing.
  115. */
  116. private void writeObject(ObjectOutputStream s) throws IOException {
  117. s.defaultWriteObject();
  118. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  119. ui.installUI(this);
  120. }
  121. }
  122. /**
  123. * Returns a string representation of this JPanel. This method
  124. * is intended to be used only for debugging purposes, and the
  125. * content and format of the returned string may vary between
  126. * implementations. The returned string may be empty but may not
  127. * be <code>null</code>.
  128. *
  129. * @return a string representation of this JPanel.
  130. */
  131. protected String paramString() {
  132. String defaultLayoutString = (defaultLayout != null ?
  133. defaultLayout.toString() : "");
  134. return super.paramString() +
  135. ",defaultLayout=" + defaultLayoutString;
  136. }
  137. /////////////////
  138. // Accessibility support
  139. ////////////////
  140. /**
  141. * Gets the AccessibleContext associated with this JPanel.
  142. * For JPanels, the AccessibleContext takes the form of an
  143. * AccessibleJPanel.
  144. * A new AccessibleJPanel instance is created if necessary.
  145. *
  146. * @return an AccessibleJPanel that serves as the
  147. * AccessibleContext of this JPanel
  148. */
  149. public AccessibleContext getAccessibleContext() {
  150. if (accessibleContext == null) {
  151. accessibleContext = new AccessibleJPanel();
  152. }
  153. return accessibleContext;
  154. }
  155. /**
  156. * This class implements accessibility support for the
  157. * <code>JPanel</code> class. It provides an implementation of the
  158. * Java Accessibility API appropriate to panel user-interface
  159. * elements.
  160. * <p>
  161. * <strong>Warning:</strong>
  162. * Serialized objects of this class will not be compatible with
  163. * future Swing releases. The current serialization support is appropriate
  164. * for short term storage or RMI between applications running the same
  165. * version of Swing. A future release of Swing will provide support for
  166. * long term persistence.
  167. */
  168. protected class AccessibleJPanel extends AccessibleJComponent {
  169. /**
  170. * Get the role of this object.
  171. *
  172. * @return an instance of AccessibleRole describing the role of the
  173. * object
  174. */
  175. public AccessibleRole getAccessibleRole() {
  176. return AccessibleRole.PANEL;
  177. }
  178. }
  179. }