FileDocCategorySizeDatePackage
FilePrinter.javaAPI DocExample4976Mon Apr 05 12:02:26 BST 1999None

FilePrinter

public class FilePrinter extends JFrame

Fields Summary
private PageFormat
mPageFormat
private FilePageRenderer
mPageRenderer
private String
mTitle
Constructors Summary
public FilePrinter()

    super("FilePrinter v1.0");
    createUI();
    PrinterJob pj = PrinterJob.getPrinterJob();
    mPageFormat = pj.defaultPage();
    setVisible(true);
  
Methods Summary
protected voidcenter()

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = getSize();
    int x = (screenSize.width - frameSize.width) / 2;
    int y = (screenSize.height - frameSize.height) / 2;
    setLocation(x, y);
  
protected voidcreateUI()

    setSize(350, 300);
    center();
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
 
    // Add the menu bar.
    JMenuBar mb = new JMenuBar();
    JMenu file = new JMenu("File", true);
    file.add(new FileOpenAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
    file.add(new FilePrintAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
    file.add(new FilePageSetupAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_P,
             Event.CTRL_MASK | Event.SHIFT_MASK));
    file.addSeparator();
    file.add(new FileQuitAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
    mb.add(file);
    JMenu page = new JMenu("Page", true);
    page.add(new PageNextPageAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
    page.add(new PagePreviousPageAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0));
    mb.add(page);
    setJMenuBar(mb);
    
    // Add the contents of the window.
    getContentPane().setLayout(new BorderLayout());

    // Exit the application when the window is closed.
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  
public static voidmain(java.lang.String[] args)

    new FilePrinter();
  
public voidshowTitle()

    int currentPage = mPageRenderer.getCurrentPage() + 1;
    int numPages = mPageRenderer.getNumPages();
    setTitle(mTitle + " - page " + currentPage + " of " + numPages);