java.lang.Object | ||||
↳ | java.awt.Component | |||
↳ | java.awt.Container | |||
↳ | javax.swing.JComponent | |||
↳ | javax.swing.JViewport |
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:
JComponents
,
if they aren't, stop and repaint the whole viewport.
Window
's graphics and
do a copyArea
on the scrolled region.
copyArea
s.
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 JavaBeansTM
has been added to the java.beans
package.
Please see XMLEncoder
.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
JViewport.AccessibleJViewport | This class implements accessibility support for the
JViewport class. |
||||||||||
JViewport.ViewListener | A listener for the view. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | BACKINGSTORE_SCROLL_MODE | Draws viewport contents into an offscreen image. | |||||||||
int | BLIT_SCROLL_MODE | Use graphics.copyArea to implement scrolling. |
|||||||||
int | SIMPLE_SCROLL_MODE | This mode uses the very simple method of redrawing the entire contents of the scrollpane each time it is scrolled. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
backingStore | This field is deprecated. As of Java 2 platform v1.3 | ||||||||||
backingStoreImage | The view image used for a backing store. | ||||||||||
isViewSizeSet | True when the viewport dimensions have been determined. | ||||||||||
lastPaintPosition | The last viewPosition that we've painted, so we know how
much of the backing store image is valid. |
||||||||||
scrollUnderway | The scrollUnderway flag is used for components like
JList . |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a
JViewport . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a
ChangeListener to the list that is
notified each time the view's
size, position, or the viewport's extent size has changed. | |||||||||||
Gets the AccessibleContext associated with this JViewport.
| |||||||||||
Returns an array of all the
ChangeListener s added
to this JViewport with addChangeListener(). | |||||||||||
Returns the size of the visible part of the view in view coordinates.
| |||||||||||
Returns an
Insets object containing this
JViewport s inset values. | |||||||||||
Returns the insets (border) dimensions as (0,0,0,0), since borders
are not supported on a
JViewport . | |||||||||||
Returns the current scrolling mode.
| |||||||||||
Returns the L&F object that renders this component.
| |||||||||||
Returns a string that specifies the name of the L&F class
that renders this component.
| |||||||||||
Returns the
JViewport 's one child or null . | |||||||||||
Returns the view coordinates that appear in the upper left
hand corner of the viewport, or 0,0 if there's no view.
| |||||||||||
Returns a rectangle whose origin is
getViewPosition
and size is getExtentSize . | |||||||||||
If the view's size hasn't been explicitly set, return the
preferred size, otherwise return the view's current size.
| |||||||||||
This method is deprecated.
As of Java 2 platform v1.3, replaced by
getScrollMode() .
| |||||||||||
The
JViewport overrides the default implementation of
this method (in JComponent ) to return false. | |||||||||||
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. | |||||||||||
Removes the
Viewport s one lightweight child. | |||||||||||
Removes a
ChangeListener from the list that's notified each
time the views size, position, or the viewports extent size
has changed. | |||||||||||
Always repaint in the parents coordinate system to make sure
only one paint is performed by the
RepaintManager . | |||||||||||
This method is deprecated.
As of JDK 5,
replaced by
Component.setBounds(int, int, int, int) .
Moves and resizes this component. | |||||||||||
Scrolls the view so that
Rectangle
within the view becomes visible. | |||||||||||
This method is deprecated.
As of Java 2 platform v1.3, replaced by
setScrollMode() .
| |||||||||||
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).
| |||||||||||
Sets the size of the visible part of the view using view coordinates.
| |||||||||||
Used to control the method of scrolling the viewport contents.
| |||||||||||
Sets the L&F object that renders this component.
| |||||||||||
Sets the
JViewport 's one lightweight child
(view ), which can be null . | |||||||||||
Sets the view coordinates that appear in the upper left
hand corner of the viewport, does nothing if there's no view.
| |||||||||||
Sets the size of the view.
| |||||||||||
Converts a point in pixel coordinates to view coordinates.
| |||||||||||
Converts a size in pixel coordinates to view coordinates.
| |||||||||||
Resets the UI property to a value from the current look and feel.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Sets the
JViewport 's one lightweight child,
which can be null . | |||||||||||
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 . | |||||||||||
Subclassers can override this to install a different
layout manager (or
null ) in the constructor. | |||||||||||
Creates a listener for the view.
| |||||||||||
Notifies listeners of a property change.
| |||||||||||
Notifies all
ChangeListeners when the views
size, position, or the viewports extent size has changed. | |||||||||||
Returns a string representation of this
JViewport . |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() |
Draws viewport contents into an offscreen image.
This was previously the default mode for JTable
.
This mode may offer advantages over "blit mode"
in some cases, but it requires a large chunk of extra RAM.
Use graphics.copyArea
to implement scrolling.
This is the fastest for most applications.
This mode uses the very simple method of redrawing the entire contents of the scrollpane each time it is scrolled. This was the default behavior in Swing 1.0 and Swing 1.1. Either of the other two options will provide better performance in most cases.
This field is 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. The default is false
.
True when the viewport dimensions have been determined. The default is false.
The last viewPosition
that we've painted, so we know how
much of the backing store image is valid.
The 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
.
Creates a JViewport
.
Adds a 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 add |
---|
Gets the AccessibleContext associated with this JViewport. For viewports, the AccessibleContext takes the form of an AccessibleJViewport. A new AccessibleJViewport instance is created if necessary.
Returns an array of all the ChangeListener
s added
to this JViewport with addChangeListener().
ChangeListener
s added or an empty
array if no listeners have been addedReturns the size of the visible part of the view in view coordinates.
Dimension
object giving the size of the view
Returns an Insets
object containing this
JViewport
s inset values. The passed-in
Insets
object will be reinitialized, and
all existing values within this object are overwritten.
insets | the Insets object which can be reused |
---|
Returns the insets (border) dimensions as (0,0,0,0), since borders
are not supported on a JViewport
.
Rectange
of zero dimension and zero originReturns the L&F object that renders this component.
ViewportUI
objectReturns a string that specifies the name of the L&F class that renders this component.
Returns the JViewport
's one child or null
.
null
if none existsReturns the view coordinates that appear in the upper left hand corner of the viewport, or 0,0 if there's no view.
Point
object giving the upper left coordinates
Returns a rectangle whose origin is 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.
If the view's size hasn't been explicitly set, return the preferred size, otherwise return the view's current size. If there is no view, return 0,0.
Dimension
object specifying the size of the view
This method is deprecated.
As of Java 2 platform v1.3, replaced by
getScrollMode()
.
Returns true
if this viewport is maintaining
an offscreen image of its contents.
true
if scrollMode
is
BACKINGSTORE_SCROLL_MODE
The 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.
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.
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.)
g | the Graphics context within which to paint
|
---|
Removes the Viewport
s one lightweight child.
child | the component to be removed |
---|
Removes a ChangeListener
from the list that's notified each
time the views size, position, or the viewports extent size
has changed.
l | the ChangeListener to remove |
---|
Always repaint in the parents coordinate system to make sure
only one paint is performed by the RepaintManager
.
tm | maximum time in milliseconds before update |
---|---|
x | the x coordinate (pixels over from left) |
y | the y coordinate (pixels down from top) |
w | the width |
h | the height |
This method is deprecated.
As of JDK 5,
replaced by Component.setBounds(int, int, int, int)
.
Moves and resizes this component.
Sets the bounds of this viewport. If the viewport's width
or height has changed, fire a StateChanged
event.
x | left edge of the origin |
---|---|
y | top edge of the origin |
w | width in pixels |
h | height in pixels |
Scrolls the view so that 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.
contentRect | the Rectangle to display |
---|
This method is deprecated.
As of Java 2 platform v1.3, replaced by
setScrollMode()
.
If true if this viewport will maintain an offscreen
image of its contents. The image is used to reduce the cost
of small one dimensional changes to the viewPosition
.
Rather than repainting the entire viewport we use
Graphics.copyArea
to effect some of the scroll.
enabled | if true, maintain an offscreen backing store |
---|
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). A non-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
.
border | the Border to set |
---|
IllegalArgumentException | this method is not implemented |
---|
Sets the size of the visible part of the view using view coordinates.
newExtent | a Dimension object specifying
the size of the view
|
---|
Used to control the method of scrolling the viewport contents. You may want to change this mode to get maximum performance for your use case.
mode | one of the following values:
|
---|
Sets the L&F object that renders this component.
ui | the ViewportUI L&F object |
---|
Sets the JViewport
's one lightweight child
(view
), which can be null
.
view | the viewport's new lightweight child |
---|
Sets the view coordinates that appear in the upper left hand corner of the viewport, does nothing if there's no view.
p | a Point object giving the upper left coordinates
|
---|
Sets the size of the view. A state changed event will be fired.
newSize | a Dimension object specifying the new
size of the view
|
---|
Converts a point in pixel coordinates to view coordinates. Subclasses of viewport that support "logical coordinates" will override this method.
p | a Point object using pixel coordinates |
---|
Point
object converted to view coordinates
Converts a size in pixel coordinates to view coordinates. Subclasses of viewport that support "logical coordinates" will override this method.
size | a Dimension object using pixel coordinates |
---|
Dimension
object converted to view coordinates
Resets the UI property to a value from the current look and feel.
Sets the 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.)
child | the lightweight child of the viewport |
---|---|
constraints | the constraints to be respected |
index | the index |
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
.
The parameters are modified
to return the values required for the blit.
dx | the horizontal delta |
---|---|
dy | the vertical delta |
blitFrom | the Point we're blitting from |
blitTo | the Point we're blitting to |
blitSize | the Dimension of the area to blit |
blitPaint | the area to blit |
Subclassers can override this to install a different
layout manager (or null
) in the constructor. Returns
the LayoutManager
to install on the JViewport
.
LayoutManager
Creates a listener for the view.
ViewListener
Notifies listeners of a property change. This is subclassed to update
the windowBlit
property.
(The putClientProperty
property is final).
propertyName | a string containing the property name |
---|---|
oldValue | the old value of the property |
newValue | the new value of the property |
Notifies all ChangeListeners
when the views
size, position, or the viewports extent size has changed.
Returns a string representation of this 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
.
JViewport