|
|
Print Preview TutorialTable of ContentsWorking with JPrintPreviewPane IntroductionMost user-friendly applications have a print preview screen that allows the user to preview pages before printing them. On the print preview screen, users typically can zoom in and out on the pages, change the arrangement of pages, change the page format, and print the pages. Although each application may have a slightly different print preview screen, the basic functionality is the same.
The core Java libraries do not provide high-level support for print preview. As a result, developers either implement their own print preview screens or disregard it entirely. By providing the basic print preview functionality, the Print Preview Library makes it easy to add print preview to an application. The library offers a general-purpose Swing component that previews pages, four layout managers that arrange pages on the screen, and a support class that helps build a print preview toolbar.
Before you start using this library, you should download and include on your classpath the jar file of the Java Look and Feel Graphics Repository. It is available free-of-charge here. Without this resource, your print preview toolbar will not have any icons or cursors. Page ContentThe Print Preview Library does not deal with the page
content. Therefore, it is not a report generation tool. The print
preview pane relies on a
That said, the library does provide a utility class,
JComponent componentToPreview = ... // application specific Pageable pageable = new ComponentPageable( componentToPreview );
Also, Side of Software offers the Report
Library, a separate reporting framework that creates
Once you have your Using a Standard ToolbarThe library offers two "standard" toolbars: Navigation and Zoom. These toolbars, which are commonly found across existing applications, can be created with a single call to a factory method. The Navigation ToolbarThe navigation toolbar allows the user to print the pages, change the page format, step through the pages one at a time, and close print preview. On a Windows machine, the toolbar may resemble:
To create a print preview pane with an accompanying
navigation toolbar, invoke one of the static
static JPanel createNavigationPanel(Pageable pageable, boolean showPrintDialog, Action closeAction, boolean largeIcons) static JPanel createNavigationPanel(PageableFactory pageableFactory, boolean showPrintDialog, Action closeAction, boolean largeIcons) The methods differ only in the first parameter. The first
method takes the preview pages (
The remaining three parameters are treated as follows:
The following code snippet creates a print preview pane with a navigation toolbar.
// create an object that will render the contents // of each page (application specific) Pageable pageable = ... // create an action that will close // print preview (application specific) Action closeAction = ... // create the print preview panel with // a navigation toolbar JPanel printPreviewPanel = PrintPreviewSupport.createNavigationPane( pageable, false, closeAction, false ); // add the panel to your application // (application specific) JComponent contentPane = ... contentPane.add( printPreviewPanel );
The Zoom ToolbarThe zoom toolbar allows the user to print the pages, change the page format, zoom in and out on the pages, change the arrangement of pages, and close print preview. There are four different zoom behaviors:
On a Windows machine, the toolbar for each of these behaviors may look like:
To create a panel with a print preview pane and a
navigation toolbar, invoke one of the static
static JPanel createZoomPanel(Pageable pageable, boolean showPrintDialog, int zoomFlavor, Action closeAction, boolean largeIcons) static JPanel createZoomPanel(PageableFactory pageableFactory, boolean showPrintDialog, int zoomFlavor, Action closeAction, boolean largeIcons) Again, the methods differ only in the first parameter. The
second method includes a page setup button on the toolbar. The
The following code snippet creates a print preview pane with a zoom toolbar.
// create an object that will render the contents // of each page (application specific) Pageable pageable = ... // create an action that will close // print preview (application specific) Action closeAction = ... // create the print preview panel with // a zoom toolbar JPanel printPreviewPanel = PrintPreviewSupport.createZoomPane( pageable, false, PrintPreviewSupport.ZOOM_IN_OUT_TOGGLE, closeAction, false ); // add the panel to your application // (application specific) JComponent contentPane = ... contentPane.add( printPreviewPanel );
Building Custom ToolbarsIf you prefer not to use the standard toolbars, you can
build your own toolbars with the help of an instance of
It supports only one instance of each of these components.
Thus, it is not possible (through
For many of the above components, you will find the following methods:
public <Component> getXxxComponent(); public Action getXxxAction(); public String getXxxText(); public Icon getXxxIcon(); public String getXxxToolTip(); public int getXxxMnemonic();
The methods return a component, action, name, icon, tool tip,
and mnemonic, respectively, appropriate for the task Custom Toolbar ExampleSuppose in your application you want to provide a minimal
print preview pane that allows the user to print all pages, select a zoom
layout, and display help information.
// create a print preview pane component Pageable pageable = ... JPrintPreviewPane printPreviewPane = new JPrintPreviewPane( pageable ); // create the print preview toolbar JToolBar toolBar = new JToolBar(); // enlist some help in creating the toolbar PrintPreviewSupport support = new PrintPreviewSupport( printPreviewPane, true ); // use the built-in print button JButton printAllButton = support.getPrintAllButton(); printAllButton.setText( "" ); // show no text printAllButton.setBorder( null ); // show no border // use the built-in zoom combobox JComboBox zoomCombo = support.getZoomComboBox( false, false ); // create our own help button (application specific) JButton helpButton = ... // add these components to the toolbar toolBar.add( printAllButton ); toolBar.addSeparator(); toolBar.add( zoomCombo ); toolBar.addSeparator(); toolBar.add( helpButton ); // place the print preview pane in a scroll pane JScrollPane scrollPane = new JScrollPane( printPreviewPane ); // add the scroll pane and toolbar to a panel JPanel panel = new JPanel( new BorderLayout() ); panel.add( scrollPane, BorderLayout.CENTER ); panel.add( toolBar, BorderLayout.PAGE_START ); The above code creates the following toolbar.
Subclassing PrintPreviewSupportYou can subclass
class CustomPrintPreviewSupport extends PrintPreviewSupport { public CustomPrintPreviewSupport( JPrintPreviewPane printPreviewPane, boolean largeIcons ) { super( printPreviewPane, largeIcons ); } public String getPrintText() { return "Print Dialog..."; } } Working with JPrintPreviewPane
Page LayoutsThe print preview pane must decide how to arrange the
pages.
These four page layouts implement
public void setLayout( LayoutManager layout ); public LayoutManager getLayout();
You can listen for changes to the layout manager by
registering a property change listener on the key
You can customize the arrangement of pages by defining
your own Zooming
public double getScale(); public boolean zoom( double scale ); public boolean zoom( double scale, Point point ); public boolean zoomIn(); public void zoomIn( int pageIndex ); public boolean zoomIn( Point point ); public boolean zoomOut(); public void zoomOut( int pageIndex ); public boolean zoomOut( Point point ); When a
When there is a change in zoom,
To determine how far the pane should zoom, it asks an
instance of
public interface ZoomFunction { public double zoomIn(double scale); public double zoomOut(double scale); }
Given the current scale, these methods return the next
scale for zooming either in or out. By returning the same value as the
parameter,
You can customize the amount of zooming the print preview
pane provides by installing a custom implementation of
public void setZoomFunction( ZoomFunction zoomFunction );
The custom implementation may either implement Modes
The following methods set and retrieve the mode:
public int getMode(); public void setMode(int mode);
You can listen for changes to the mode by registering a property change listener on the key MODE_PROPERTY. Page SelectionWhen
public ListSelectionModel getPageSelectionModel(); public boolean isPageSelected( int pageIndex ); public void setPageSelectionModel( ListSelectionModel selectionModel ); public boolean getPageSelectionAllowed(); public int getSelectedPageIndex(); public void setPageSelectionAllowed( boolean pageSelectionAllowed );
You can listen for changes to the selection by registering
a Page NumberingWhen page numbering is turned on, the print preview pane displays a page number for each page. You can turn page numbering on and off with
public void setPageNumbersAreShown( boolean pageNumbersAreShown ); The page number is rendered using an instance of
public void setPageNumberRenderer( PageNumberRenderer pageNumberRenderer ); Page number renderers must implement a method that returns a component ready to render the page number: public interface PageNumberRenderer { public Component getPageNumberRendererComponent( JPrintPreviewPane printPreviewPane, int pageNumber, boolean isSelected, boolean pageHasFocus ); } The library provides one implementation of
Here is a an example of a custom page number renderer that
overrides
class CustomPageNumberRenderer extends DefaultPageNumberRenderer { protected String pageNumberToString( int pageNumber ) { return "Page " + pageNumber; } } Custom Preview PagesYou can change the appearance of each previewed page. By
default, the print preview pane creates a Customizing the Look and FeelYou can further customize your print preview pane by defining look-and-feel properties. At application start-up, define the properties you wish to change with the call
UIManager.put( propertyKey, propertyValue );
where
ExampleThe example PrintPreviewDemo demonstrates the flexibility of print preview panes. It displays pages of text beneath a custom tool bar and allows the user to add and remove pages, change the look and feel of the pages, and zoom in and out. In this application, you can find examples of how to
You can run this sample application on the library’s homepage.
Home | Contact Us | Privacy Policy
Copyright © 2016 Side of Software, a branch of Natavision. All rights reserved.
|