javax.swing

Class JViewport

Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable

public class JViewport
extends JComponent
implements Accessible

                                                     _
   +-------------------------------+    ...........Y1 \
   |  view                         |                .  \
   |  (this component's child)     |                .   > VY
   |                               |                .  / = Y2-Y1
   |         +------------------------------+  ....Y2_/
   |         | viewport            |        |       .
   |         | (this component)    |        |       .
   |         |                     |        |       .
   |         |                     |        |       .
   |         |                     |        |       .
   |         |                     |        |       .
   |         +------------------------------+  ....Y3
   |                               |                .
   |         .                     |        .       .
   |         .                     |        .       .
   +---------.---------------------+    ...........Y4
   .         .                     .        .
   .         .                     .        .
   .         .                     .        .
   X1.......X2.....................X3.......X4
   \____  ___/
        \/
        VX = X2-X1

A viewport is, like all swing components, located at some position in the swing component tree; that location is exactly the same as any other components: the viewport's "bounds".

But in terms of drawing its child, the viewport thinks of itself as covering a particular position of the view's coordinate space. For example, the getViewPosition() method returns the position (VX,VY) shown above, which is an position in "view space", even though this is implemented by positioning the underlying child at position (-VX,-VY)

See Also:
Serialized Form

Nested Class Summary

protected class
JViewport.AccessibleJViewport
Provides accessibility support for JViewport.
protected class
JViewport.ViewListener
A ComponentListener that listens for changes of the view's size.

Nested classes/interfaces inherited from class javax.swing.JComponent

JComponent.AccessibleJComponent

Nested classes/interfaces inherited from class java.awt.Container

Container.AccessibleAWTContainer

Nested classes/interfaces inherited from class java.awt.Component

Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

static int
BACKINGSTORE_SCROLL_MODE
static int
BLIT_SCROLL_MODE
static int
SIMPLE_SCROLL_MODE
protected boolean
backingStore
Deprecated. since JDK 1.3
protected Image
backingStoreImage
The backingstore image used for the backingstore and blit scroll methods.
protected boolean
isViewSizeSet
protected Point
lastPaintPosition
The position at which the view has been drawn the last time.
protected boolean
scrollUnderway

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

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()
Default constructor for subclasses.

Method Summary

void
addChangeListener(ChangeListener listener)
protected void
addImpl(Component comp, Object constraints, int index)
protected boolean
computeBlit(int dx, int dy, Point blitFrom, Point blitTo, Dimension blitSize, Rectangle blitPaint)
Computes the parameters for the blitting scroll method.
protected LayoutManager
createLayoutManager()
Creates the LayoutManager that is used for this viewport.
protected JViewport.ViewListener
createViewListener()
Creates a JViewport.ViewListener that is supposed to listen for size changes on the view component.
protected void
fireStateChanged()
AccessibleContext
getAccessibleContext()
Returns the accessible context for this JViewport.
ChangeListener[]
getChangeListeners()
Dimension
getExtentSize()
Insets
getInsets()
Insets
getInsets(Insets insets)
int
getScrollMode()
ViewportUI
getUI()
This method returns the viewport's UI delegate.
String
getUIClassID()
This method returns the String ID of the UI class of Separator.
Component
getView()
Point
getViewPosition()
Get the viewport's position in view space.
Rectangle
getViewRect()
Dimension
getViewSize()
Returns the viewSize when set, or the preferred size of the set Component view.
boolean
isBackingStoreEnabled()
Deprecated. 1.4
boolean
isOptimizedDrawingEnabled()
Overridden to return false, so the JViewport's paint method gets called instead of directly calling the children.
void
paint(Graphics g)
Paints this component on the screen.
void
removeChangeListener(ChangeListener listener)
void
repaint(long tm, int x, int y, int w, int h)
Forward repaint to parent to make sure only one paint is performed by the RepaintManager.
void
reshape(int x, int y, int w, int h)
Deprecated. use Component.setBounds(int,int,int,int) instead
void
scrollRectToVisible(Rectangle contentRect)
Scrolls the view so that contentRect becomes visible.
void
setBackingStoreEnabled(boolean b)
Deprecated. 1.4
void
setBorder(Border border)
void
setExtentSize(Dimension newSize)
void
setScrollMode(int mode)
void
setUI(ViewportUI ui)
This method sets the viewport's UI delegate.
void
setView(Component v)
void
setViewPosition(Point p)
void
setViewSize(Dimension newSize)
Dimension
toViewCoordinates(Dimension size)
Point
toViewCoordinates(Point p)
void
updateUI()
This method resets the UI used to the Look and Feel defaults..

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

BACKINGSTORE_SCROLL_MODE

public static final int BACKINGSTORE_SCROLL_MODE
Field Value:
2

BLIT_SCROLL_MODE

public static final int BLIT_SCROLL_MODE
Field Value:
1

SIMPLE_SCROLL_MODE

public static final int SIMPLE_SCROLL_MODE
Field Value:
0

backingStore

protected boolean backingStore

Deprecated. since JDK 1.3

This flag indicates whether we use a backing store for drawing.

backingStoreImage

protected Image backingStoreImage
The backingstore image used for the backingstore and blit scroll methods.

isViewSizeSet

protected boolean isViewSizeSet

lastPaintPosition

protected Point lastPaintPosition
The position at which the view has been drawn the last time. This is used to determine the bittable area.

scrollUnderway

protected boolean scrollUnderway

Constructor Details

JViewport

public JViewport()
Default constructor for subclasses. When Component is extended directly, it forms a lightweight component that must be hosted in an opaque native container higher in the tree.

Method Details

addChangeListener

public void addChangeListener(ChangeListener listener)

addImpl

protected void addImpl(Component comp,
                       Object constraints,
                       int index)
Overrides:
addImpl in interface Container

computeBlit

protected boolean computeBlit(int dx,
                              int dy,
                              Point blitFrom,
                              Point blitTo,
                              Dimension blitSize,
                              Rectangle blitPaint)
Computes the parameters for the blitting scroll method. dx and dy specifiy the X and Y offset by which the viewport is scrolled. All other arguments are output parameters and are filled by this method. blitFrom holds the position of the blit rectangle in the viewport rectangle before scrolling, blitTo where the blitArea is copied to. blitSize holds the size of the blit area and blitPaint is the area of the view that needs to be painted. This method returns true if blitting is possible and false if the viewport has to be repainted completetly without blitting.
Parameters:
dx - the horizontal delta
dy - the vertical delta
blitFrom - the position from where to blit; set by this method
blitTo - the position where to blit area is copied to; set by this method
blitSize - the size of the blitted area; set by this method
blitPaint - the area that needs repainting; set by this method
Returns:
true if blitting is possible, false otherwise

createLayoutManager

protected LayoutManager createLayoutManager()
Creates the LayoutManager that is used for this viewport. Override this method if you want to use a custom LayoutManager.
Returns:
a LayoutManager to use for this viewport

createViewListener

protected JViewport.ViewListener createViewListener()
Creates a JViewport.ViewListener that is supposed to listen for size changes on the view component.
Returns:
a ViewListener instance

fireStateChanged

protected void fireStateChanged()

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the accessible context for this JViewport. This will be an instance of JViewport.AccessibleJViewport.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface JComponent
Returns:
the accessible context for this JViewport

getChangeListeners

public ChangeListener[] getChangeListeners()

getExtentSize

public Dimension getExtentSize()

getInsets

public final Insets getInsets()
Overrides:
getInsets in interface JComponent

getInsets

public final Insets getInsets(Insets insets)
Overrides:
getInsets in interface JComponent

getScrollMode

public int getScrollMode()

getUI

public ViewportUI getUI()
This method returns the viewport's UI delegate.
Returns:
The viewport's UI delegate.

getUIClassID

public String getUIClassID()
This method returns the String ID of the UI class of Separator.
Overrides:
getUIClassID in interface JComponent
Returns:
The UI class' String ID.

getView

public Component getView()

getViewPosition

public Point getViewPosition()
Get the viewport's position in view space. Despite confusing name, this really does return the viewport's (0,0) position in view space, not the view's position.

getViewRect

public Rectangle getViewRect()

getViewSize

public Dimension getViewSize()
Returns the viewSize when set, or the preferred size of the set Component view. If no viewSize and no Component view is set an empty Dimension is returned.

isBackingStoreEnabled

public boolean isBackingStoreEnabled()

Deprecated. 1.4


isOptimizedDrawingEnabled

public boolean isOptimizedDrawingEnabled()
Overridden to return false, so the JViewport's paint method gets called instead of directly calling the children. This is necessary in order to get a useful clipping and translation on the children.
Overrides:
isOptimizedDrawingEnabled in interface JComponent
Returns:
false

paint

public void paint(Graphics g)
Paints this component on the screen. The clipping region in the graphics context will indicate the region that requires painting. This is called whenever the component first shows, or needs to be repaired because something was temporarily drawn on top. It is not necessary for subclasses to call super.paint(g). Components with no area are not painted.
Overrides:
paint in interface JComponent
Parameters:
g - the graphics context for this paint job

removeChangeListener

public void removeChangeListener(ChangeListener listener)

repaint

public void repaint(long tm,
                    int x,
                    int y,
                    int w,
                    int h)
Forward repaint to parent to make sure only one paint is performed by the RepaintManager.
Overrides:
repaint in interface JComponent
Parameters:
tm - number of milliseconds to defer the repaint request
x - the X coordinate of the upper left corner of the dirty area
y - the Y coordinate of the upper left corner of the dirty area
w - the width of the dirty area
h - the height of the dirty area

reshape

public void reshape(int x,
                    int y,
                    int w,
                    int h)

Deprecated. use Component.setBounds(int,int,int,int) instead

Sets the bounding rectangle for this component to the specified values. Note that these coordinates are relative to the parent, not to the screen.
Overrides:
reshape in interface JComponent
Parameters:
x - the X coordinate of the upper left corner of the rectangle
y - the Y coordinate of the upper left corner of the rectangle

scrollRectToVisible

public void scrollRectToVisible(Rectangle contentRect)
Scrolls the view so that contentRect becomes visible.
Overrides:
scrollRectToVisible in interface JComponent
Parameters:
contentRect - the rectangle to make visible within the view

setBackingStoreEnabled

public void setBackingStoreEnabled(boolean b)

Deprecated. 1.4


setBorder

public final void setBorder(Border border)
Overrides:
setBorder in interface JComponent

setExtentSize

public void setExtentSize(Dimension newSize)

setScrollMode

public void setScrollMode(int mode)

setUI

public void setUI(ViewportUI ui)
This method sets the viewport's UI delegate.
Parameters:
ui - The viewport's UI delegate.

setView

public void setView(Component v)

setViewPosition

public void setViewPosition(Point p)

setViewSize

public void setViewSize(Dimension newSize)

toViewCoordinates

public Dimension toViewCoordinates(Dimension size)

toViewCoordinates

public Point toViewCoordinates(Point p)

updateUI

public void updateUI()
This method resets the UI used to the Look and Feel defaults..
Overrides:
updateUI in interface JComponent

JViewport.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.