APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JViewport

extends JComponent
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Known Direct Subclasses
javax.swing.plaf.basic.BasicTabbedPaneUI.ScrollableTabViewport
Annotations
@SuppressWarnings:serial
Imports
java.awt.*, java.awt.event.*, java.awt.geom.AffineTransform, java.awt.image.AbstractMultiResolutionImage, .ImageObserver, java.awt.peer.ComponentPeer, java.beans.BeanProperty, .Transient, javax.swing.plaf.ViewportUI, javax.swing.event.*, javax.swing.border.*, javax.accessibility.*, java.io.Serializable, java.util.Arrays, .Collections, sun.awt.AWTAccessor, sun.swing.SwingUtilities2

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 are 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

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 java.beans.XMLEncoder.

Authors
Hans Muller, Philip Milne
Since
1.2
See Also
JScrollPane

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JViewport.AccessibleJViewport

This class implements accessibility support for the JViewport class.

pack-priv static class
protected class
JViewport.ViewListener

A listener for the view.

Field Summary

Modifier and TypeField and Description
protected boolean
backingStore

Deprecated As of Java 2 platform v1.3
True when this viewport is maintaining an offscreen image of its contents, so that some scrolling can take place using fast "bit-blit" operations instead of by accessing the view object to construct the display.
public static final int
BACKINGSTORE_SCROLL_MODE

Draws viewport contents into an offscreen image.

protected transient Image
backingStoreImage

The view image used for a backing store.

public static final int
BLIT_SCROLL_MODE

Use graphics.copyArea to implement scrolling.

private transient ChangeEvent
pack-priv static final Object
EnableWindowBlit

Property used to indicate window blitting should not be done.

private boolean
hasHadValidView

Whether or not a valid view has been installed.

private transient boolean
inBlitPaint

Set to true in paintView when paint is invoked.

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.

private transient boolean
repaintAll

This is set to true in setViewPosition if doing a window blit and the viewport is obscured.

private transient Timer
repaintTimer

Instead of directly invoking repaint, a Timer is started and when it fires, repaint is invoked.

private int
protected boolean
scrollUnderway

The scrollUnderway flag is used for components like JList.

public static final int
SIMPLE_SCROLL_MODE

This mode uses the very simple method of redrawing the entire contents of the scrollpane each time it is scrolled.

private static final String
private boolean
viewChanged

When view is changed we have to synchronize scrollbar values with viewport (see the BasicScrollPaneUI#syncScrollPaneWithViewport method).

private ComponentListener
private transient boolean
waitingForRepaint

This is set to true in paint, if repaintAll is true and the clip rectangle does not match the bounds.

Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JViewport()

Creates a JViewport.

Method Summary

Modifier and TypeMethod and Description
public void
addChangeListener(ChangeListener
the ChangeListener to add
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
the lightweight child of the viewport
child
,
Object
the constraints to be respected
constraints
,
int
the index
index
)

Overrides java.awt.Container.addImpl.

Sets the JViewport's one lightweight child, which can be null.

private void
blitDoubleBuffered(JComponent view, Graphics g, int clipX, int clipY, int clipW, int clipH, int blitFromX, int blitFromY, int blitToX, int blitToY, int blitW, int blitH)

private boolean
canUseWindowBlitter()

Returns true if the viewport is not obscured by one of its ancestors, or its ancestors children and if the viewport is showing.

protected boolean

Returns:

true if the parameters are modified and we're ready to blit; false otherwise
computeBlit
(int
the horizontal delta
dx
,
int
the vertical delta
dy
,
Point
the Point we're blitting from
blitFrom
,
Point
the Point we're blitting to
blitTo
,
Dimension
the Dimension of the area to blit
blitSize
,
Rectangle
the area to blit
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

Returns:

a LayoutManager
createLayoutManager
()

Subclassers can override this to install a different layout manager (or null) in the constructor.

private Timer
private Image
createScaledImage(final int width, final int height, int scaledWidth, int scaledHeight)

protected JViewport.ViewListener

Returns:

a ViewListener
createViewListener
()

Creates a listener for the view.

protected void
firePropertyChange(String
a string containing the property name
propertyName
,
Object
the old value of the property
oldValue
,
Object
the new value of the property
newValue
)

Overrides java.awt.Component.firePropertyChange.

Notifies listeners of a property change.

protected void
fireStateChanged()

Notifies all ChangeListeners when the views size, position, or the viewports extent size has changed.

private void
flushViewDirtyRegion(Graphics
the Graphics context within which to paint
g
,
Rectangle dirty)

If the repaint manager has a dirty region for the view, the view is asked to paint.

public AccessibleContext

Returns:

an AccessibleJViewport that serves as the AccessibleContext of this JViewport
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JViewport.

private Graphics
public ChangeListener[]

Returns:

all of the ChangeListeners added or an empty array if no listeners have been added
getChangeListeners
()

Returns an array of all the ChangeListeners added to this JViewport with addChangeListener().

public Dimension

Returns:

a Dimension object giving the size of the view
getExtentSize
()

Returns the size of the visible part of the view in view coordinates.

public final Insets

Returns:

a Rectangle of zero dimension and zero origin
getInsets
()

Overrides javax.swing.JComponent.getInsets.

Returns the insets (border) dimensions as (0,0,0,0), since borders are not supported on a JViewport.

public final Insets

Returns:

this viewports inset values
getInsets
(Insets
the Insets object which can be reused
insets
)

Overrides javax.swing.JComponent.getInsets.

Returns an Insets object containing this JViewports inset values.

public int

Returns:

the scrollMode property
getScrollMode
()

Returns the current scrolling mode.

public ViewportUI

Returns:

a ViewportUI object
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

public String

Returns:

the string "ViewportUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns a string that specifies the name of the L&F class that renders this component.

public Component

Returns:

the viewports child, or null if none exists
getView
()

Returns the JViewport's one child or null.

private Point
getViewLocation()

Only used by the paint method below.

public Point

Returns:

a Point object giving the upper left coordinates
getViewPosition
()

Returns the view coordinates that appear in the upper left hand corner of the viewport, or 0,0 if there's no view.

public Rectangle

Returns:

a Rectangle giving the visible part of the view using view coordinates.
getViewRect
()

Returns a rectangle whose origin is getViewPosition and size is getExtentSize.

public Dimension

Returns:

a Dimension object specifying the size of the view
getViewSize
()

If the view's size hasn't been explicitly set, return the preferred size, otherwise return the view's current size.

public boolean

Returns:

true if scrollMode is BACKINGSTORE_SCROLL_MODE
isBackingStoreEnabled
()
Deprecated As of Java 2 platform v1.3, replaced by getScrollMode().

Returns true if this viewport is maintaining an offscreen image of its contents.

private boolean
private boolean
public boolean

Returns:

false
isOptimizedDrawingEnabled
()

Overrides javax.swing.JComponent.isOptimizedDrawingEnabled.

The JViewport overrides the default implementation of this method (in JComponent) to return false.

protected boolean

Returns:

true if scroll mode is a BACKINGSTORE_SCROLL_MODE.
isPaintingOrigin
()

Overrides javax.swing.JComponent.isPaintingOrigin.

Returns true if scroll mode is a BACKINGSTORE_SCROLL_MODE to cause painting to originate from JViewport, or one of its ancestors.

private boolean
needsRepaintAfterBlit()

Returns true if the component needs to be completely repainted after a blit and a paint is received.

public void
paint(Graphics
the Graphics context within which to paint
g
)

Overrides javax.swing.JComponent.paint.

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.

private void
private void
private void
paintView(Graphics
the Graphics context within which to paint
g
)

Called to paint the view, usually when blitPaint can not blit.

protected String

Returns:

a string representation of this JViewport
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JViewport.

private int
positionAdjustment(int parentWidth, int childWidth, int childAt)

public void
remove(Component child)

Overrides java.awt.Container.remove.

Removes the Viewports one lightweight child.

public void
removeChangeListener(ChangeListener
the ChangeListener to remove
l
)

Removes a ChangeListener from the list that's notified each time the views size, position, or the viewports extent size has changed.

public void
repaint(long
maximum time in milliseconds before update
tm
,
int
the x coordinate (pixels over from left)
x
,
int
the y coordinate (pixels down from top)
y
,
int
the width
w
,
int
the height
h
)

Overrides javax.swing.JComponent.repaint.

Always repaint in the parents coordinate system to make sure only one paint is performed by the RepaintManager.

public void
reshape(int
left edge of the origin
x
,
int
top edge of the origin
y
,
int
width in pixels
w
,
int
height in pixels
h
)

Overrides javax.swing.JComponent.reshape.

Sets the bounds of this viewport.

public void
scrollRectToVisible(Rectangle
the Rectangle to display
contentRect
)

Overrides javax.swing.JComponent.scrollRectToVisible.

Scrolls the view so that Rectangle within the view becomes visible.

public void
setBackingStoreEnabled(boolean
if true, maintain an offscreen backing store
enabled
)
Deprecated As of Java 2 platform v1.3, replaced by setScrollMode().

If true if this viewport will maintain an offscreen image of its contents.

public final void
setBorder(Border
the Border to set
border
)

Overrides javax.swing.JComponent.setBorder.

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).

public void
setExtentSize(Dimension
a Dimension object specifying the size of the view
newExtent
)

Sets the size of the visible part of the view using view coordinates.

public void
setScrollMode(int
one of the following values:
  • JViewport.BLIT_SCROLL_MODE
  • JViewport.BACKINGSTORE_SCROLL_MODE
  • JViewport.SIMPLE_SCROLL_MODE
mode
)

Used to control the method of scrolling the viewport contents.

public void
setUI(ViewportUI
the ViewportUI L&F object
ui
)

Sets the L&F object that renders this component.

public void
setView(Component
the viewport's new lightweight child
view
)

Sets the JViewport's one lightweight child (view), which can be null.

public void
setViewPosition(Point
a Point object giving the upper left coordinates
p
)

Sets the view coordinates that appear in the upper left hand corner of the viewport, does nothing if there's no view.

public void
setViewSize(Dimension
a Dimension object specifying the new size of the view
newSize
)

Sets the size of the view.

public Dimension

Returns:

a Dimension object converted to view coordinates
toViewCoordinates
(Dimension
a Dimension object using pixel coordinates
size
)

Converts a size in pixel coordinates to view coordinates.

public Point

Returns:

a Point object converted to view coordinates
toViewCoordinates
(Point
a Point object using pixel coordinates
p
)

Converts a point in pixel coordinates to view coordinates.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

private void
validateView()

Ascends the Viewport's parents stopping when a component is found that returns true to isValidateRoot.

private boolean

Returns:

true if blitting succeeded; otherwise false
windowBlitPaint
(Graphics
the Graphics context within which to paint
g
)

Used when blitting.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisPaintingisPaintingForPrintisPaintingTileisRequestFocusEnabledisValidateRootpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsrevalidatesafelyGetGraphicssafelyGetGraphicssetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate