javax.swing
Class JViewport

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JViewport
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

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
protected  boolean backingStore
          Deprecated. since JDK 1.3
static int BACKINGSTORE_SCROLL_MODE
           
protected  Image backingStoreImage
          The backingstore image used for the backingstore and blit scroll methods.
static int BLIT_SCROLL_MODE
           
protected  boolean isViewSizeSet
           
protected  Point lastPaintPosition
          The position at which the view has been drawn the last time.
protected  boolean scrollUnderway
           
static int SIMPLE_SCROLL_MODE
           
 
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()
           
 
Method Summary
 void addChangeListener(ChangeListener listener)
           
protected  void addImpl(Component comp, Object constraints, int index)
          This method is called by all the add() methods to perform the actual adding of the component.
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()
          Get the component's insets, which are calculated from the JComponent.border property.
 Insets getInsets(Insets insets)
          Get the component's insets, which are calculated from the JComponent.border property.
 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)
          Paint the component.
 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)
          Moves and resizes the component.
 void scrollRectToVisible(Rectangle contentRect)
          Scrolls the view so that contentRect becomes visible.
 void setBackingStoreEnabled(boolean b)
          Deprecated. 1.4
 void setBorder(Border border)
          Set the value of the JComponent.border property.
 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, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, 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, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, 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, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, 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, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, 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, hide, 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, processMouseEvent, 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, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SIMPLE_SCROLL_MODE

public static final int SIMPLE_SCROLL_MODE
See Also:
Constant Field Values

BLIT_SCROLL_MODE

public static final int BLIT_SCROLL_MODE
See Also:
Constant Field Values

BACKINGSTORE_SCROLL_MODE

public static final int BACKINGSTORE_SCROLL_MODE
See Also:
Constant Field Values

scrollUnderway

protected boolean scrollUnderway

isViewSizeSet

protected boolean isViewSizeSet

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.


lastPaintPosition

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

Constructor Detail

JViewport

public JViewport()
Method Detail

getExtentSize

public Dimension getExtentSize()

toViewCoordinates

public Dimension toViewCoordinates(Dimension size)

toViewCoordinates

public Point toViewCoordinates(Point p)

setExtentSize

public void setExtentSize(Dimension newSize)

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.


setViewSize

public void setViewSize(Dimension newSize)

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.


setViewPosition

public void setViewPosition(Point p)

getViewRect

public Rectangle getViewRect()

isBackingStoreEnabled

public boolean isBackingStoreEnabled()
Deprecated. 1.4


setBackingStoreEnabled

public void setBackingStoreEnabled(boolean b)
Deprecated. 1.4


setScrollMode

public void setScrollMode(int mode)

getScrollMode

public int getScrollMode()

getView

public Component getView()

setView

public void setView(Component v)

reshape

public void reshape(int x,
                    int y,
                    int w,
                    int h)
Description copied from class: JComponent
Moves and resizes the component.

Overrides:
reshape in class JComponent
Parameters:
x - the new horizontal location
y - the new vertial location
w - the new width
h - the new height

getInsets

public final Insets getInsets()
Description copied from class: JComponent
Get the component's insets, which are calculated from the JComponent.border property. If the border is null, calls Container.getInsets().

Overrides:
getInsets in class JComponent
Returns:
The component's current insets

getInsets

public final Insets getInsets(Insets insets)
Description copied from class: JComponent
Get the component's insets, which are calculated from the JComponent.border property. If the border is null, calls Container.getInsets(). The passed-in Insets value will be used as the return value, if possible.

Overrides:
getInsets in class JComponent
Parameters:
insets - Return value object to reuse, if possible
Returns:
The component's current insets

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 class JComponent
Returns:
false

paint

public void paint(Graphics g)
Description copied from class: JComponent

Paint the component. This is a delicate process, and should only be called from the repaint thread, under control of the RepaintManager. Client code should usually call Component.repaint() to trigger painting.

The body of the paint call involves calling JComponent.paintComponent(java.awt.Graphics), JComponent.paintBorder(java.awt.Graphics), and JComponent.paintChildren(java.awt.Graphics) in order. If you want to customize painting behavior, you should override one of these methods rather than paint.

For more details on the painting sequence, see this article.

Overrides:
paint in class JComponent
Parameters:
g - The graphics context to paint with
See Also:
JComponent.paintImmediately(Rectangle)

addChangeListener

public void addChangeListener(ChangeListener listener)

removeChangeListener

public void removeChangeListener(ChangeListener listener)

getChangeListeners

public ChangeListener[] getChangeListeners()

getUIClassID

public String getUIClassID()
This method returns the String ID of the UI class of Separator.

Overrides:
getUIClassID in class JComponent
Returns:
The UI class' String ID.
See Also:
JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

updateUI

public void updateUI()
This method resets the UI used to the Look and Feel defaults..

Overrides:
updateUI in class JComponent

getUI

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

Returns:
The viewport's UI delegate.

setUI

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

Parameters:
ui - The viewport's UI delegate.

setBorder

public final void setBorder(Border border)
Description copied from class: JComponent
Set the value of the JComponent.border property.

Overrides:
setBorder in class JComponent
Parameters:
border - The new value of the property
See Also:
JComponent.getBorder()

scrollRectToVisible

public void scrollRectToVisible(Rectangle contentRect)
Scrolls the view so that contentRect becomes visible.

Overrides:
scrollRectToVisible in class JComponent
Parameters:
contentRect - the rectangle to make visible within the view

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 class JComponent
Returns:
the accessible context for this JViewport

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 class 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
See Also:
Component.update(Graphics)

addImpl

protected void addImpl(Component comp,
                       Object constraints,
                       int index)
Description copied from class: Container
This method is called by all the add() methods to perform the actual adding of the component. Subclasses who wish to perform their own processing when a component is added should override this method. Any subclass doing this must call the superclass version of this method in order to ensure proper functioning of the container.

Overrides:
addImpl in class Container
Parameters:
comp - The component to be added.
constraints - The layout constraints for this component, or null if there are no constraints.
index - The index in the component list to insert this child at, or -1 to add at the end of the list.

fireStateChanged

protected void fireStateChanged()

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

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

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