public class JPrintPreview extends JPanel
The pages are rendered into an internal image buffer, for best performance. The tradeoff is that a lot of memory is required for large zoom factors, so you should limit it within a reasonable range.
The given Printable
must be able to render the same page several times, but
does not need to be able to give random access to all the pages.
JPanel.AccessibleJPanel
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JPrintPreview()
Creates an empty
JPrintPreview pane. |
JPrintPreview(Pageable pageable)
Creates a
JPrintPreview pane and initialize it with a Pageable
object. |
JPrintPreview(Printable printable,
PageFormat format)
Creates a
JPrintPreview pane and initialize it with a Printable
object, which is to be shown in the given PageFormat . |
Modifier and Type | Method and Description |
---|---|
protected Image |
createImage()
Creates an
Image of the page that is currently to be printed. |
protected void |
drawPaper(Graphics g,
int width,
int height)
Draws the paper.
|
int |
getPage()
Gets the page number of the currently shown page.
|
double |
getZoomFactor()
Gets the current zoom factor.
|
protected void |
initZoom(PageFormat format)
Initializes the zoom factor.
|
boolean |
isLowMem()
Returns if a low memory footprint is required.
|
protected void |
recreate()
Recreates the internal image cache.
|
void |
setLowMem(boolean val)
Sets if a low memory footprint is required.
|
void |
setPage(int page)
Sets the page number of the page to be shown.
|
void |
setPageable(Pageable pageable)
Sets a
Pageable to be shown. |
void |
setPrintable(Printable printable,
PageFormat format)
Sets a
Printable to be shown in the given PageFormat . |
void |
setZoomFactor(double zoom)
Sets the current zoom factor.
|
void |
turnBack()
Turns back one page.
|
void |
turnForward()
Turns forward one page.
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, 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, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, 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, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, 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, 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, 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, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public JPrintPreview()
JPrintPreview
pane. It will show nothing.public JPrintPreview(Printable printable, PageFormat format)
JPrintPreview
pane and initialize it with a Printable
object, which is to be shown in the given PageFormat
.printable
- Printable
to be shownformat
- PageFormat
to be usedpublic JPrintPreview(Pageable pageable)
JPrintPreview
pane and initialize it with a Pageable
object.pageable
- Pageable
to be shownpublic void setPrintable(Printable printable, PageFormat format)
Printable
to be shown in the given PageFormat
. The first
page of the Printable
will be shown with a standard zoom factor.
You can always change to other Printable
and Pageable
objects.
printable
- Printable
to be shownformat
- PageFormat
to be usedpublic void setPageable(Pageable pageable)
Pageable
to be shown. The first page of the Pageable
will be
shown with a standard zoom factor.
You can always change to other Printable
and Pageable
objects.
pageable
- Pageable
to be shownpublic void setLowMem(boolean val)
val
- true: low memorypublic boolean isLowMem()
protected void initZoom(PageFormat format)
Component
. If the Component
has currently no size (i.e. is not shown), a default size of 500x500 pixels will be
used. You can change the default behaviour by overriding this method.format
- PageFormat
to be used.protected void drawPaper(Graphics g, int width, int height)
The paper must start at the coordinates (0,0). There is no scaling set yet, and antialiasing is disabled here.
g
- Graphics
. It is safe to cast it to Graphics2D
.width
- Width of the paper. The actual image is at least three pixels broader,
to give space for the shadow.height
- Height of the paper. The actual image is at least three pixels taller,
to give space for the shadow.protected void recreate() throws PrinterException
Note that this method may require some time and memory.
PrinterException
protected Image createImage() throws PrinterException
Image
of the page that is currently to be printed. The image
will show the page content, scaled to the current zoom factor.
Note that this method may require some time and memory.
Image
containing the printed and scaled page.PrinterException
- Could not print to this image, e.g. if the current page does not exist.public int getPage()
public void setPage(int page) throws PrinterException
NOTE: some Printables are unable to give random access to the printed document. You cannot go back to a page that has been printed already, or go forward by more than one page.
page
- New page index (starting from 0).PrinterException
public void turnForward() throws PrinterException
PrinterException
public void turnBack() throws PrinterException
Printable
is unable to go backward, weird things may happen.PrinterException
public double getZoomFactor()
public void setZoomFactor(double zoom)
Note that this class creates an internal Image
representation of the
current page, for performance reasons. It will consume a lot of memory on large
zoom factors! Setting setLowMem(boolean)
to true
could help
you save some memory.
zoom
- The new zoom factor.Copyright © 2004–2016. All rights reserved.