Extreme Component Documentation

Use JPrintPreview and PrintFactory in JComponentPack

May 13, 2008

JPrintPreview is a Java Print Preview component in JComponentPack.

PrintFactory is a utility class in com.zfqjava.swing package to generate printable in JComponentPack.

PrintFactory usage.

createPrintable(Component comp, Map map)
createPrintable(Image image, Map map)
It need two parameters, the first is a instance of Image or a instance of Component, the second parameter is a instance of Map, the key of map could be all the properties of class PrintFactory.
For example:
Key: PrintFactory.MARGIN, describe the margin of page. Its value type must be java.awt.Insets, for example: new java.awt.Insets(5,5,5,5);
Key: PrintFactory.HORIZONTAL_ALIGNMENT, describe the alignment of horizontal its value will be SwingConstants.LEFT or SwingConstants.RIGHT or SwingConstants.CENTER.
Key: PrintFactory.FOOTER_FONT, describe the font of footer, its value type need be java.awt.Font.
Key: PrintFactory.FOOTER_ICON, describe the icon of footer, its value type need be javax.swing.Icon.
Key: PrintFactory.FOOTER_FORMAT, describe the message format of footer, its value type need be java.text.MessageFormat.
Key: PrintFactory.FOOTER_TEXT_ALIGNMENT, describe the alignment of footer text, its value will be SwingConstants.LEFT or SwingConstants.RIGHT or SwingConstants.CENTER.
Key: PrintFactory.FOOTER_ICON_ALIGNMENT, describe the alignment of footer icon, its value will be SwingConstants.LEFT or SwingConstants.RIGHT or SwingConstants.CENTER.
Key: PrintFactory.SHOW_FOOTER_LINE, indicate whether show footer line. Its value will be true of false.
Key: PrintFactory.TABLE_SHOW_ROW_HEADER, Identifies whether show the row header of table. Its value type is java.lang.Boolean.
Key: PrintFactory.TABLE_SHOW_GRID_LINE, Identifies whether show the grid line of table. Its value type is java.lang.Boolean.
Key: PrintFactory.TABLE_PRINT_AREA, describe the area of table will be print , its value type is com.zfqjava.swing.cell.Cell.
Key: PrintFactory.FIT_WIDTH, Identifies whether auto fit page width, its value type is java.lang.Boolean.
Key: PrintFactory.KEEP_ASPECT_RADIO, Identifies keep the aspect radio for fit width or fit height
How to set footer icon and its alignment, use following statements is ok.,
 public Image getImage(URL url) {
      try {
          return ImageIO.read(url);
      } catch(Exception e) {
          e.printStackTrace();
          return null;
      }
}
URL url = getClass().getResource("/com/zfqjava/client/resources/images/logo.png");
  
 Map.put(PrintFactory.FOOTER_ICON, new ImageIcon(getImage(url)));
Map.put(PrintFactory.FOOTER_ICON_ALIGNMENT, SwingConstants.LEFT);

JPrintPreview usage.

Example code:
JPrintPreview printPreview = new JPrintPreview();
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
printPreview.setPageFormat(pageFormat);
printPreview.setPrintData(PrintFactory.createPrintable(image,map));
//how to use PrintFactory, please see above.
printPreview.showFrame();

JPrintPreview could set page format, see the example code. For more information, please see java.awt.print.PageFormat.

JPrintPreview have 7 methods to show:

The following three methods show the preview in the current window of parameter.

showInternalFrame(JInternalFrame internalFrame)
showFrame(JFrame frame)
showApplet(Applet applet)
The following four methods show the preview in a new window.
showFrame()
showFrame(String title)
showDialog(Component parent, String title)
showDialog(Component parent)

Subscribe RSS >>download

Send Us Feedback

The hottest article