javax.swing
Class JViewport

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JViewport
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable
public class JViewport
extends JComponent
implements Accessible

The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.

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:

  1. The view and parent view and checked to see if they are JComponents, if they aren't, stop and repaint the whole viewport.
  2. If the viewport is obscured by an ancestor, stop and repaint the whole viewport.
  3. Compute the region that will become visible, if it is as big as the viewport, stop and repaint the whole view region.
  4. Obtain the ancestor Window's graphics and do a copyArea on the scrolled region.
  5. Message the view to repaint the newly visible region.
  6. The next time paint is invoked on the viewport, if the clip region is smaller than the viewport size a timer is kicked off to repaint the whole region.
In general this approach is much faster. Compared to the backing store approach this avoids the overhead of maintaining an offscreen buffer and having to do two 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: 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 JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

See Also:
JScrollPane

 

Nested Class Summary

protected  class JViewport.AccessibleJViewport
    This class implements accessibility support for the JViewport class.
protected  class JViewport.ViewListener
    A listener for the view.
 
Nested classes inherited from class javax.swing.JComponent
 
Nested classes inherited from class java.awt.Container
 
Nested classes inherited from class java.awt.Component
Component.BltBufferStrategy, Component.FlipBufferStrategy
 

 

Field Summary

protected  boolean backingStore
    Deprecated. As of Java 2 platform v1.3
static int BACKINGSTORE_SCROLL_MODE
    Draws viewport contents into an offscreen image.
protected  Image backingStoreImage
    The view image used for a backing store.
static int BLIT_SCROLL_MODE
    Use graphics.copyArea to implement scrolling.
protected  boolean isViewSizeSet
    True when the viewport dimensions have been determined.
protected  Point lastPaintPosition
    The last viewPosition that we've painted, so we know how much of the backing store image is valid.
protected  boolean scrollUnderway
    The scrollUnderway flag is used for components like JList.
static int SIMPLE_SCROLL_MODE
    This mode uses the very simple method of redrawing the entire contents of the scrollpane each time it is scrolled.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 

 

Constructor Summary

JViewport()
    Creates a JViewport.
 

 

Method Summary

 void addChangeListener(ChangeListener l)
    Adds a ChangeListener to 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 be null.
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 oldLoc in the upper left hand corner and we're scrolling to newLoc.
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 ChangeListeners when 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 Insets object containing this JViewports 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 or null.
 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 getViewPosition and size is getExtentSize.
 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 JViewport overrides the default implementation of this method (in JComponent) to return false.
 void paint(Graphics g)
    Depending on whether the 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.
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 ChangeListener from 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 Rectangle within 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 be null.
 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.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, revalidate, setActionMap, setAlignmentX, setAlignmentY,