ScrollPane.java revision 3261
2362N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * A container class which implements automatic horizontal and/or 0N/A * vertical scrolling for a single child component. The display 0N/A * policy for the scrollbars can be set to: 0N/A * <LI>as needed: scrollbars created and shown only when needed by scrollpane 0N/A * <LI>always: scrollbars created and always shown by the scrollpane 0N/A * <LI>never: scrollbars never created or shown by the scrollpane 0N/A * The state of the horizontal and vertical scrollbars is represented 0N/A * by two <code>ScrollPaneAdjustable</code> objects (one for each 0N/A * dimension) which implement the <code>Adjustable</code> interface. 0N/A * The API provides methods to access those objects such that the 0N/A * attributes on the Adjustable object (such as unitIncrement, value, 0N/A * etc.) can be manipulated. 0N/A * Certain adjustable properties (minimum, maximum, blockIncrement, 0N/A * and visibleAmount) are set internally by the scrollpane in accordance 0N/A * with the geometry of the scrollpane and its child and these should 0N/A * not be set by programs using the scrollpane. 0N/A * If the scrollbar display policy is defined as "never", then the 0N/A * scrollpane can still be programmatically scrolled using the 0N/A * setScrollPosition() method and the scrollpane will move and clip 0N/A * the child's contents appropriately. This policy is useful if the 0N/A * program needs to create and manage its own adjustable controls. 0N/A * The placement of the scrollbars is controlled by platform-specific 0N/A * properties set by the user outside of the program. 0N/A * The initial size of this container is set to 100x100, but can 0N/A * be reset using setSize(). 0N/A * Scrolling with the wheel on a wheel-equipped mouse is enabled by default. 0N/A * This can be disabled using <code>setWheelScrollingEnabled</code>. 0N/A * Wheel scrolling can be customized by setting the block and 0N/A * unit increment of the horizontal and vertical Adjustables. 0N/A * For information on how mouse wheel events are dispatched, see 0N/A * the class description for {@link MouseWheelEvent}. 0N/A * Insets are used to define any space used by scrollbars and any 0N/A * borders created by the scroll pane. getInsets() can be used 0N/A * to get the current value for the insets. If the value of 0N/A * scrollbarsAlwaysVisible is false, then the value of the insets 0N/A * will change dynamically depending on whether the scrollbars are 0N/A * currently visible or not. 0N/A * @author Amy Fowler 0N/A * @author Tim Prinzing 0N/A * Initialize JNI field and method IDs 0N/A /* ensure that the necessary native libraries are loaded */ 0N/A * only when the size of the child exceeds the size of the scrollpane 0N/A * shown regardless of the respective sizes of the scrollpane and child. 0N/A * regardless of the respective sizes of the scrollpane and child. 0N/A * There are 3 ways in which a scroll bar can be displayed. 0N/A * This integer will represent one of these 3 displays - 0N/A * (SCROLLBARS_ALWAYS, SCROLLBARS_AS_NEEDED, SCROLLBARS_NEVER) 0N/A * @see #getScrollbarDisplayPolicy 0N/A * An adjustable vertical scrollbar. 0N/A * It is important to note that you must <em>NOT</em> call 3 0N/A * <code>Adjustable</code> methods, namely: 0N/A * <code>setMinimum()</code>, <code>setMaximum()</code>, 0N/A * <code>setVisibleAmount()</code>. 0N/A * @see #getVAdjustable 0N/A * An adjustable horizontal scrollbar. 0N/A * It is important to note that you must <em>NOT</em> call 3 0N/A * <code>Adjustable</code> methods, namely: 0N/A * <code>setMinimum()</code>, <code>setMaximum()</code>, 0N/A * <code>setVisibleAmount()</code>. 0N/A * @see #getHAdjustable 0N/A * Indicates whether or not scrolling should take place when a 0N/A * MouseWheelEvent is received. 0N/A * JDK 1.1 serialVersionUID 0N/A * Create a new scrollpane container with a scrollbar display 0N/A * policy of "as needed". 0N/A * @throws HeadlessException if GraphicsEnvironment.isHeadless() 0N/A * @see java.awt.GraphicsEnvironment#isHeadless 0N/A * Create a new scrollpane container. 0N/A * @param scrollbarDisplayPolicy policy for when scrollbars should be shown 0N/A * @throws IllegalArgumentException if the specified scrollbar 0N/A * display policy is invalid 0N/A * @throws HeadlessException if GraphicsEnvironment.isHeadless() 0N/A * @see java.awt.GraphicsEnvironment#isHeadless 0N/A * Construct a name for this component. Called by getName() when the 0N/A // The scrollpane won't work with a windowless child... it assumes 0N/A // it is moving a child window around so the windowless child is 0N/A // wrapped with a window. 0N/A * Adds the specified component to this scroll pane container. 0N/A * If the scroll pane has an existing child component, that 0N/A * component is removed and the new one is added. 0N/A * @param comp the component to be added 0N/A * @param constraints not applicable 0N/A * @param index position of child component (must be <= 0) 0N/A * Returns the display policy for the scrollbars. 0N/A * @return the display policy for the scrollbars 0N/A * Returns the current size of the scroll pane's view port. 0N/A * @return the size of the view port in pixels 0N/A * Returns the height that would be occupied by a horizontal 0N/A * scrollbar, which is independent of whether it is currently 0N/A * displayed by the scroll pane or not. 0N/A * @return the height of a horizontal scrollbar in pixels 0N/A * Returns the width that would be occupied by a vertical 0N/A * scrollbar, which is independent of whether it is currently 0N/A * displayed by the scroll pane or not. 0N/A * @return the width of a vertical scrollbar in pixels 0N/A * Returns the <code>ScrollPaneAdjustable</code> object which 0N/A * represents the state of the vertical scrollbar. 0N/A * The declared return type of this method is 0N/A * <code>Adjustable</code> to maintain backward compatibility. 0N/A * @see java.awt.ScrollPaneAdjustable 0N/A * Returns the <code>ScrollPaneAdjustable</code> object which 0N/A * represents the state of the horizontal scrollbar. 0N/A * The declared return type of this method is 0N/A * <code>Adjustable</code> to maintain backward compatibility. 0N/A * @see java.awt.ScrollPaneAdjustable 0N/A * Scrolls to the specified position within the child component. 0N/A * A call to this method is only valid if the scroll pane contains 0N/A * a child. Specifying a position outside of the legal scrolling bounds 0N/A * of the child will scroll to the closest legal position. 0N/A * Legal bounds are defined to be the rectangle: 0N/A * x = 0, y = 0, width = (child width - view port width), 0N/A * height = (child height - view port height). 0N/A * This is a convenience method which interfaces with the Adjustable 0N/A * objects which represent the state of the scrollbars. 0N/A * @param x the x position to scroll to 0N/A * @param y the y position to scroll to 0N/A * @throws NullPointerException if the scrollpane does not contain 0N/A * Scrolls to the specified position within the child component. 0N/A * A call to this method is only valid if the scroll pane contains 0N/A * a child and the specified position is within legal scrolling bounds 0N/A * of the child. Specifying a position outside of the legal scrolling 0N/A * bounds of the child will scroll to the closest legal position. 0N/A * Legal bounds are defined to be the rectangle: 0N/A * x = 0, y = 0, width = (child width - view port width), 0N/A * height = (child height - view port height). 0N/A * This is a convenience method which interfaces with the Adjustable 0N/A * objects which represent the state of the scrollbars. 0N/A * @param p the Point representing the position to scroll to 0N/A * @throws NullPointerException if {@code p} is {@code null} 0N/A * Returns the current x,y position within the child which is displayed 0N/A * at the 0,0 location of the scrolled panel's view port. 0N/A * This is a convenience method which interfaces with the adjustable 0N/A * objects which represent the state of the scrollbars. 0N/A * @return the coordinate position for the current scroll position 0N/A * @throws NullPointerException if the scrollpane does not contain 0N/A * Sets the layout manager for this container. This method is 0N/A * overridden to prevent the layout mgr from being set. 0N/A * @param mgr the specified layout manager 0N/A * Lays out this container by resizing its child to its preferred size. 0N/A * If the new preferred size of the child causes the current scroll 0N/A * position to be invalid, the scroll position is set to the closest 0N/A * @see Component#validate 0N/A * Determine the size to allocate the child component. 0N/A * If the viewport area is bigger than the childs 0N/A * preferred size then the child is allocated enough 0N/A * to fill the viewport, otherwise the child is given 0N/A * it's preferred size. 0N/A // calculate the view size, accounting for border but not scrollbars 0N/A // on whether or not scrollbars were displayed on last resize 0N/A // determine whether or not horz or vert scrollbars will be displayed 0N/A }
else {
// SCROLLBARS_NEVER 0N/A // adjust predicted view size to account for scrollbars 0N/A // if child is smaller than view, size it up 0N/A * @deprecated As of JDK version 1.1, 0N/A * replaced by <code>doLayout()</code>. 0N/A // update adjustables... the viewport size may have changed 0N/A // with the scrollbars coming or going so the viewport size 0N/A // is updated before the adjustables. 0N/A * Prints the component in this scroll pane. 0N/A * @param g the specified Graphics window 0N/A * @see Component#print 0N/A * @see Component#printAll 0N/A * Creates the scroll pane's peer. 0N/A // Bug 4124460. Save the current adjustable values, 0N/A // so they can be restored after addnotify. Set the 0N/A // adjustables to 0, to prevent crashes for possible 0N/A // Bug 4124460. Restore the adjustable values. 0N/A * Returns a string representing the state of this 0N/A * <code>ScrollPane</code>. This 0N/A * method is intended to be used only for debugging purposes, and the 0N/A * content and format of the returned string may vary between 0N/A * implementations. The returned string may be empty but may not be 0N/A * <code>null</code>. 0N/A * @return the parameter string of this scroll pane 0N/A * Process mouse wheel events that are delivered to this 0N/A * <code>ScrollPane</code> by scrolling an appropriate amount. 0N/A * <p>Note that if the event parameter is <code>null</code> 0N/A * the behavior is unspecified and may result in an 0N/A * @param e the mouse wheel event 0N/A * If wheel scrolling is enabled, we return true for MouseWheelEvents 0N/A * Wheel scrolling is enabled by default. * @param handleWheel <code>true</code> if scrolling should be done * automatically for a MouseWheelEvent, * <code>false</code> otherwise. * @see #isWheelScrollingEnabled * @see java.awt.event.MouseWheelEvent * @see java.awt.event.MouseWheelListener * Indicates whether or not scrolling will take place in response to * the mouse wheel. Wheel scrolling is enabled by default. * @see #setWheelScrollingEnabled(boolean) * Writes default serializable fields to stream. // 4352819: We only need this degenerate writeObject to make // it safe for future versions of this class to write optional * Reads default serializable fields to stream. * @exception HeadlessException if * <code>GraphicsEnvironment.isHeadless()</code> returns * @see java.awt.GraphicsEnvironment#isHeadless // 4352819: Gotcha! Cannot use s.defaultReadObject here and // then continue with reading optional data. Use GetField instead. // // Note to future maintainers // if (f.defaulted("wheelScrollingEnabled")) { // // We are reading pre-1.4 stream that doesn't have // // optional data, not even the TC_ENDBLOCKDATA marker. // // Reading anything after this point is unsafe as we will // // read unrelated objects further down the stream (4352819). // // Reading data from 1.4 or later, it's ok to try to read // // optional data as OptionalDataException with eof == true // // will be correctly reported * Invoked when the value of the adjustable has changed. * Gets the AccessibleContext associated with this ScrollPane. * For scroll panes, the AccessibleContext takes the form of an * AccessibleAWTScrollPane. * A new AccessibleAWTScrollPane instance is created if necessary. * @return an AccessibleAWTScrollPane that serves as the * AccessibleContext of this ScrollPane * This class implements accessibility support for the * <code>ScrollPane</code> class. It provides an implementation of the * Java Accessibility API appropriate to scroll pane user-interface * JDK 1.3 serialVersionUID * Get the role of this object. * @return an instance of AccessibleRole describing the role of the }
// class AccessibleAWTScrollPane * In JDK 1.1.1, the pkg private class java.awt.PeerFixer was moved to * become an inner class of ScrollPane, which broke serialization * for ScrollPane objects using JDK 1.1. * Instead of moving it back out here, which would break all JDK 1.1.x * releases, we keep PeerFixer in both places. Because of the scoping rules, * the PeerFixer that is used in ScrollPane will be the one that is the * inner class. This pkg private PeerFixer class below will only be used * if the Java 2 platform is used to deserialize ScrollPane objects that were serialized * Invoked when the value of the adjustable has changed.