public class JViewport extends JComponent implements Accessible
 By default, JViewport is opaque. To change this, use the
 setOpaque method.
 
NOTE:We have implemented a faster scrolling algorithm that does not require a buffer to draw in. The algorithm works as follows:
JComponents,
 if they aren't, stop and repaint the whole viewport.
 Window's graphics and
 do a copyArea on the scrolled region.
 copyAreas.
 Compared to the non backing store case this
 approach will greatly reduce the painted region.
 This approach can cause slower times than the backing store approach when the viewport is obscured by another window, or partially offscreen. When another window obscures the viewport the copyArea will copy garbage and a paint event will be generated by the system to inform us we need to paint the newly exposed region. The only way to handle this is to repaint the whole viewport, which can cause slower performance than the backing store case. In most applications very rarely will the user be scrolling while the viewport is obscured by another window or offscreen, so this optimization is usually worth the performance hit when obscured.
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
 Warning:
 Serialized objects of this class will not be compatible with
 future Swing releases. The current serialization support is
 appropriate for short term storage or RMI between applications running
 the same version of Swing.  As of 1.4, support for long term storage
 of all JavaBeans™
 has been added to the java.beans package.
 Please see XMLEncoder.
JScrollPane| Modifier and Type | Class and Description | 
|---|---|
| protected class  | JViewport.AccessibleJViewportThis class implements accessibility support for the
  JViewportclass. | 
| protected class  | JViewport.ViewListenerA listener for the view. | 
JComponent.AccessibleJComponentContainer.AccessibleAWTContainerComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy| Modifier and Type | Field and Description | 
|---|---|
| protected boolean | backingStoreDeprecated. 
 As of Java 2 platform v1.3 | 
| static int | BACKINGSTORE_SCROLL_MODEDraws viewport contents into an offscreen image. | 
| protected Image | backingStoreImageThe view image used for a backing store. | 
| static int | BLIT_SCROLL_MODEUse  graphics.copyAreato implement scrolling. | 
| protected boolean | isViewSizeSetTrue when the viewport dimensions have been determined. | 
| protected Point | lastPaintPositionThe last  viewPositionthat we've painted, so we know how
 much of the backing store image is valid. | 
| protected boolean | scrollUnderwayThe  scrollUnderwayflag is used for components likeJList. | 
| static int | SIMPLE_SCROLL_MODEThis mode uses the very simple method of redrawing the entire
 contents of the scrollpane each time it is scrolled. | 
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description | 
|---|
| JViewport()Creates a  JViewport. | 
| Modifier and Type | Method and Description | 
|---|---|
| void | addChangeListener(ChangeListener l)Adds a  ChangeListenerto the list that is
 notified each time the view's
 size, position, or the viewport's extent size has changed. | 
| protected void | addImpl(Component child,
       Object constraints,
       int index)Sets the  JViewport's one lightweight child,
 which can benull. | 
| protected boolean | computeBlit(int dx,
           int dy,
           Point blitFrom,
           Point blitTo,
           Dimension blitSize,
           Rectangle blitPaint)Computes the parameters for a blit where the backing store image
 currently contains  oldLocin the upper left hand corner
 and we're scrolling tonewLoc. | 
| protected LayoutManager | createLayoutManager()Subclassers can override this to install a different
 layout manager (or  null) in the constructor. | 
| protected JViewport.ViewListener | createViewListener()Creates a listener for the view. | 
| protected void | firePropertyChange(String propertyName,
                  Object oldValue,
                  Object newValue)Notifies listeners of a property change. | 
| protected void | fireStateChanged()Notifies all  ChangeListenerswhen the views
 size, position, or the viewports extent size has changed. | 
| AccessibleContext | getAccessibleContext()Gets the AccessibleContext associated with this JViewport. | 
| ChangeListener[] | getChangeListeners()Returns an array of all the  ChangeListeners added
 to this JViewport with addChangeListener(). | 
| Dimension | getExtentSize()Returns the size of the visible part of the view in view coordinates. | 
| Insets | getInsets()Returns the insets (border) dimensions as (0,0,0,0), since borders
 are not supported on a  JViewport. | 
| Insets | getInsets(Insets insets)Returns an  Insetsobject containing thisJViewports inset values. | 
| int | getScrollMode()Returns the current scrolling mode. | 
| ViewportUI | getUI()Returns the L&F object that renders this component. | 
| String | getUIClassID()Returns a string that specifies the name of the L&F class
 that renders this component. | 
| Component | getView()Returns the  JViewport's one child ornull. | 
| Point | getViewPosition()Returns the view coordinates that appear in the upper left
 hand corner of the viewport, or 0,0 if there's no view. | 
| Rectangle | getViewRect()Returns a rectangle whose origin is  getViewPositionand size isgetExtentSize. | 
| Dimension | getViewSize()If the view's size hasn't been explicitly set, return the
 preferred size, otherwise return the view's current size. | 
| boolean | isBackingStoreEnabled()Deprecated. 
 As of Java 2 platform v1.3, replaced by
              getScrollMode(). | 
| boolean | isOptimizedDrawingEnabled()The  JViewportoverrides the default implementation of
 this method (inJComponent) to return false. | 
| protected boolean | isPaintingOrigin()Returns true if scroll mode is a  BACKINGSTORE_SCROLL_MODEto cause
 painting to originate fromJViewport, or one of its
 ancestors. | 
| void | paint(Graphics g)Depending on whether the  backingStoreis enabled,
 either paint the image through the backing store or paint
 just the recently exposed part, using the backing store
 to "blit" the remainder. | 
| protected String | paramString()Returns a string representation of this  JViewport. | 
| void | remove(Component child)Removes the  Viewports one lightweight child. | 
| void | removeChangeListener(ChangeListener l)Removes a  ChangeListenerfrom the list that's notified each
 time the views size, position, or the viewports extent size
 has changed. | 
| void | repaint(long tm,
       int x,
       int y,
       int w,
       int h)Always repaint in the parents coordinate system to make sure
 only one paint is performed by the  RepaintManager. | 
| void | reshape(int x,
       int y,
       int w,
       int h)Sets the bounds of this viewport. | 
| void | scrollRectToVisible(Rectangle contentRect)Scrolls the view so that  Rectanglewithin the view becomes visible. | 
| void | setBackingStoreEnabled(boolean enabled)Deprecated. 
 As of Java 2 platform v1.3, replaced by
              setScrollMode(). | 
| void | setBorder(Border border)The viewport "scrolls" its child (called the "view") by the
 normal parent/child clipping (typically the view is moved in
 the opposite direction of the scroll). | 
| void | setExtentSize(Dimension newExtent)Sets the size of the visible part of the view using view coordinates. | 
| void | setScrollMode(int mode)Used to control the method of scrolling the viewport contents. | 
| void | setUI(ViewportUI ui)Sets the L&F object that renders this component. | 
| void | setView(Component view)Sets the  JViewport's one lightweight child
 (view), which can benull. | 
| void | setViewPosition(Point p)Sets the view coordinates that appear in the upper left
 hand corner of the viewport, does nothing if there's no view. | 
| void | setViewSize(Dimension newSize)Sets the size of the view. | 
| Dimension | toViewCoordinates(Dimension size)Converts a size in pixel coordinates to view coordinates. | 
| Point | toViewCoordinates(Point p)Converts a point in pixel coordinates to view coordinates. | 
| void | updateUI()Resets the UI property to a value from the current look and feel. | 
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateadd, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeaction, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycleprotected boolean isViewSizeSet
protected Point lastPaintPosition
viewPosition that we've painted, so we know how
 much of the backing store image is valid.@Deprecated protected boolean backingStore
false.setScrollMode(int)protected transient Image backingStoreImage
protected boolean scrollUnderway
scrollUnderway flag is used for components like
 JList.  When the downarrow key is pressed on a
 JList and the selected
 cell is the last in the list, the scrollpane autoscrolls.
 Here, the old selected cell needs repainting and so we need
 a flag to make the viewport do the optimized painting
 only when there is an explicit call to
 setViewPosition(Point).
 When setBounds is called through other routes,
 the flag is off and the view repaints normally.  Another approach
 would be to remove this from the JViewport
 class and have the JList manage this case by using
 setBackingStoreEnabled.  The default is
 false.public static final int BLIT_SCROLL_MODE
graphics.copyArea to implement scrolling.
 This is the fastest for most applications.setScrollMode(int), 
Constant Field Valuespublic static final int BACKINGSTORE_SCROLL_MODE
JTable.
 This mode may offer advantages over "blit mode"
 in some cases, but it requires a large chunk of extra RAM.setScrollMode(int), 
Constant Field Valuespublic static final int SIMPLE_SCROLL_MODE
setScrollMode(int), 
Constant Field Valuespublic ViewportUI getUI()
ViewportUI objectpublic void setUI(ViewportUI ui)
ui - the ViewportUI L&F objectUIDefaults.getUI(javax.swing.JComponent)public void updateUI()
updateUI in class JComponentJComponent.updateUI()public String getUIClassID()
getUIClassID in class JComponentJComponent.getUIClassID(), 
UIDefaults.getUI(javax.swing.JComponent)protected void addImpl(Component child, Object constraints, int index)
JViewport's one lightweight child,
 which can be null.
 (Since there is only one child which occupies the entire viewport,
 the constraints and index
 arguments are ignored.)addImpl in class Containerchild - the lightweight child of the viewportconstraints - the constraints to be respectedindex - the indexsetView(java.awt.Component)public void remove(Component child)
Viewports one lightweight child.remove in class Containerchild - the component to be removedsetView(java.awt.Component)public void scrollRectToVisible(Rectangle contentRect)
Rectangle
 within the view becomes visible.
 
 This attempts to validate the view before scrolling if the
 view is currently not valid - isValid returns false.
 To avoid excessive validation when the containment hierarchy is
 being created this will not validate if one of the ancestors does not
 have a peer, or there is no validate root ancestor, or one of the
 ancestors is not a Window or Applet.
 
 Note that this method will not scroll outside of the
 valid viewport; for example, if contentRect is larger
 than the viewport, scrolling will be confined to the viewport's
 bounds.
scrollRectToVisible in class JComponentcontentRect - the Rectangle to displayJComponent.isValidateRoot(), 
Component.isValid(), 
Component.getPeer()public final void setBorder(Border border)
null border,
 or non-zero insets, isn't supported, to prevent the geometry
 of this component from becoming complex enough to inhibit
 subclassing.  To create a JViewport with a border,
 add it to a JPanel that has a border.
 Note:  If border is non-null, this
 method will throw an exception as borders are not supported on
 a JViewPort.
setBorder in class JComponentborder - the Border to setIllegalArgumentException - this method is not implementedBorder, 
CompoundBorderpublic final Insets getInsets()
JViewport.getInsets in class JComponentRectangle of zero dimension and zero originsetBorder(javax.swing.border.Border)public final Insets getInsets(Insets insets)
Insets object containing this
 JViewports inset values.  The passed-in
 Insets object will be reinitialized, and
 all existing values within this object are overwritten.getInsets in class JComponentinsets - the Insets object which can be reusedgetInsets()public boolean isOptimizedDrawingEnabled()
JViewport overrides the default implementation of
 this method (in JComponent) to return false.
 This ensures
 that the drawing machinery will call the Viewport's
 paint
 implementation rather than messaging the JViewport's
 children directly.isOptimizedDrawingEnabled in class JComponentprotected boolean isPaintingOrigin()
BACKINGSTORE_SCROLL_MODE to cause
 painting to originate from JViewport, or one of its
 ancestors. Otherwise returns false.isPaintingOrigin in class JComponentBACKINGSTORE_SCROLL_MODE.JComponent.isPaintingOrigin()public void paint(Graphics g)
backingStore is enabled,
 either paint the image through the backing store or paint
 just the recently exposed part, using the backing store
 to "blit" the remainder.
 The term "blit" is the pronounced version of the PDP-10 BLT (BLock Transfer) instruction, which copied a block of bits. (In case you were curious.)
paint in class JComponentg - the Graphics context within which to paintJComponent.paintComponent(java.awt.Graphics), 
JComponent.paintBorder(java.awt.Graphics), 
JComponent.paintChildren(java.awt.Graphics), 
JComponent.getComponentGraphics(java.awt.Graphics), 
JComponent.repaint(long, int, int, int, int)public void reshape(int x,
                    int y,
                    int w,
                    int h)
StateChanged event.reshape in class JComponentx - left edge of the originy - top edge of the originw - width in pixelsh - height in pixelsJComponent.reshape(int, int, int, int)public void setScrollMode(int mode)
mode - one of the following values:
 BLIT_SCROLL_MODE, 
BACKINGSTORE_SCROLL_MODE, 
SIMPLE_SCROLL_MODEpublic int getScrollMode()
scrollMode propertysetScrollMode(int)@Deprecated public boolean isBackingStoreEnabled()
getScrollMode().true if this viewport is maintaining
 an offscreen image of its contents.true if scrollMode is
    BACKINGSTORE_SCROLL_MODE@Deprecated public void setBackingStoreEnabled(boolean enabled)
setScrollMode().viewPosition.
 Rather than repainting the entire viewport we use
 Graphics.copyArea to effect some of the scroll.enabled - if true, maintain an offscreen backing storepublic Component getView()
JViewport's one child or null.null if none existssetView(java.awt.Component)public void setView(Component view)
JViewport's one lightweight child
 (view), which can be null.view - the viewport's new lightweight childgetView()public Dimension getViewSize()
Dimension object specifying the size of the viewpublic void setViewSize(Dimension newSize)
newSize - a Dimension object specifying the new
          size of the viewpublic Point getViewPosition()
Point object giving the upper left coordinatespublic void setViewPosition(Point p)
p - a Point object giving the upper left coordinatespublic Rectangle getViewRect()
getViewPosition
 and size is getExtentSize.
 This is the visible part of the view, in view coordinates.Rectangle giving the visible part of
          the view using view coordinates.protected boolean computeBlit(int dx,
                              int dy,
                              Point blitFrom,
                              Point blitTo,
                              Dimension blitSize,
                              Rectangle blitPaint)
oldLoc in the upper left hand corner
 and we're scrolling to newLoc.
 The parameters are modified
 to return the values required for the blit.dx - the horizontal deltady - the vertical deltablitFrom - the Point we're blitting fromblitTo - the Point we're blitting toblitSize - the Dimension of the area to blitblitPaint - the area to blitpublic Dimension getExtentSize()
Dimension object giving the size of the viewpublic Dimension toViewCoordinates(Dimension size)
size - a Dimension object using pixel coordinatesDimension object converted to view coordinatespublic Point toViewCoordinates(Point p)
p - a Point object using pixel coordinatesPoint object converted to view coordinatespublic void setExtentSize(Dimension newExtent)
newExtent - a Dimension object specifying
          the size of the viewprotected JViewport.ViewListener createViewListener()
ViewListenerprotected LayoutManager createLayoutManager()
null) in the constructor.  Returns
 the LayoutManager to install on the JViewport.LayoutManagerpublic void addChangeListener(ChangeListener l)
ChangeListener to the list that is
 notified each time the view's
 size, position, or the viewport's extent size has changed.l - the ChangeListener to addremoveChangeListener(javax.swing.event.ChangeListener), 
setViewPosition(java.awt.Point), 
setViewSize(java.awt.Dimension), 
setExtentSize(java.awt.Dimension)public void removeChangeListener(ChangeListener l)
ChangeListener from the list that's notified each
 time the views size, position, or the viewports extent size
 has changed.l - the ChangeListener to removeaddChangeListener(javax.swing.event.ChangeListener)public ChangeListener[] getChangeListeners()
ChangeListeners added
 to this JViewport with addChangeListener().ChangeListeners added or an empty
         array if no listeners have been addedprotected void fireStateChanged()
ChangeListeners when the views
 size, position, or the viewports extent size has changed.public void repaint(long tm,
                    int x,
                    int y,
                    int w,
                    int h)
RepaintManager.repaint in class JComponenttm - maximum time in milliseconds before updatex - the x coordinate (pixels over from left)y - the y coordinate (pixels down from top)w - the widthh - the heightComponent.update(java.awt.Graphics)protected String paramString()
JViewport.
 This method
 is intended to be used only for debugging purposes, and the
 content and format of the returned string may vary between
 implementations. The returned string may be empty but may not
 be null.paramString in class JComponentJViewportprotected void firePropertyChange(String propertyName, Object oldValue, Object newValue)
windowBlit property.
 (The putClientProperty property is final).firePropertyChange in class ComponentpropertyName - a string containing the property nameoldValue - the old value of the propertynewValue - the new value of the propertypublic AccessibleContext getAccessibleContext()
getAccessibleContext in interface AccessiblegetAccessibleContext in class Component Submit a bug or feature 
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
 Copyright © 1993, 2017, Oracle and/or its affiliates.  All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.