Print the text file and print preview them : Print « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » PrintScreenshots 
Print the text file and print preview them
Print the text file and print preview them


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

public class FilePrinter extends JFrame {
  private PageFormat pageFormat;

  private FilePageRenderer pageRenderer;

  private String title;

  public FilePrinter() {
    super();
    init();
    PrinterJob pj = PrinterJob.getPrinterJob();
    pageFormat = pj.defaultPage();
    setVisible(true);
  }

  protected void init() {
    setSize(350300);
    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);

    getContentPane().setLayout(new BorderLayout());

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  protected void center() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = getSize();
    int x = (screenSize.width - frameSize.width2;
    int y = (screenSize.height - frameSize.height2;
    setLocation(x, y);
  }

  public void showTitle() {
    int currentPage = pageRenderer.getCurrentPage() 1;
    int numPages = pageRenderer.getNumPages();
    setTitle(title + " - page " + currentPage + " of " + numPages);
  }

  public class FileOpenAction extends AbstractAction {
    public FileOpenAction() {
      super("Open...");
    }

    public void actionPerformed(ActionEvent ae) {
      // Pop up a file dialog.
      JFileChooser fc = new JFileChooser(".");
      int result = fc.showOpenDialog(FilePrinter.this);
      if (result != 0) {
        return;
      }
      java.io.File f = fc.getSelectedFile();
      if (f == null) {
        return;
      }
      // Load the specified file.
      try {
        pageRenderer = new FilePageRenderer(f, pageFormat);
        title = "[" + f.getName() "]";
        showTitle();
        JScrollPane jsp = new JScrollPane(pageRenderer);
        getContentPane().removeAll();
        getContentPane().add(jsp, BorderLayout.CENTER);
        validate();
      catch (java.io.IOException ioe) {
        System.out.println(ioe);
      }
    }
  }
  public static void main(String[] args) {
    new FilePrinter();
  }

  public class FilePrintAction extends AbstractAction {
    public FilePrintAction() {
      super("Print");
    }

    public void actionPerformed(ActionEvent ae) {
      PrinterJob pj = PrinterJob.getPrinterJob();
      pj.setPrintable(pageRenderer, pageFormat);
      if (pj.printDialog()) {
        try {
          pj.print();
        catch (PrinterException e) {
          System.out.println(e);
        }
      }
    }
  }

  public class FilePageSetupAction extends AbstractAction {
    public FilePageSetupAction() {
      super("Page setup...");
    }

    public void actionPerformed(ActionEvent ae) {
      PrinterJob pj = PrinterJob.getPrinterJob();
      pageFormat = pj.pageDialog(pageFormat);
      if (pageRenderer != null) {
        pageRenderer.pageInit(pageFormat);
        showTitle();
      }
    }
  }

  public class FileQuitAction extends AbstractAction {
    public FileQuitAction() {
      super("Quit");
    }
    public void actionPerformed(ActionEvent ae) {
      System.exit(0);
    }
  }

  public class PageNextPageAction extends AbstractAction {
    public PageNextPageAction() {
      super("Next page");
    }

    public void actionPerformed(ActionEvent ae) {
      if (pageRenderer != null)
        pageRenderer.nextPage();
      showTitle();
    }
  }

  public class PagePreviousPageAction extends AbstractAction {
    public PagePreviousPageAction() {
      super("Previous page");
    }

    public void actionPerformed(ActionEvent ae) {
      if (pageRenderer != null)
        pageRenderer.previousPage();
      showTitle();
    }
  }
  class FilePageRenderer extends JComponent implements Printable {
    private int currentPageIndex;

    private Vector lineVector;

    private Vector pageVector;

    private Font font;

    private int fontSize;

    private Dimension preferredSize;

    public FilePageRenderer(File file, PageFormat pageFormat)
        throws IOException {
      fontSize = 12;
      font = new Font("Serif", Font.PLAIN, fontSize);
      BufferedReader in = new BufferedReader(new FileReader(file));
      String line;
      lineVector = new Vector();
      while ((line = in.readLine()) != null)
        lineVector.addElement(line);
      in.close();
      pageInit(pageFormat);
    }

    public void pageInit(PageFormat pageFormat) {
      currentPageIndex = 0;
      pageVector = new Vector();
      float y = fontSize;
      Vector page = new Vector();
      for (int i = 0; i < lineVector.size(); i++) {
        String line = (StringlineVector.elementAt(i);
        page.addElement(line);
        y += fontSize;
        if (y + fontSize * > pageFormat.getImageableHeight()) {
          y = 0;
          pageVector.addElement(page);
          page = new Vector();
        }
      }

      if (page.size() 0)
        pageVector.addElement(page);

      preferredSize = new Dimension((intpageFormat.getImageableWidth(),
          (intpageFormat.getImageableHeight());
      repaint();
    }

    public void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2Dg;
      java.awt.geom.Rectangle2D r = new java.awt.geom.Rectangle2D.Float(00,
          preferredSize.width, preferredSize.height);
      g2.setPaint(Color.white);
      g2.fill(r);
      Vector page = (VectorpageVector.elementAt(currentPageIndex);

      g2.setFont(font);
      g2.setPaint(Color.black);
      float x = 0;
      float y = fontSize;
      for (int i = 0; i < page.size(); i++) {
        String line = (Stringpage.elementAt(i);
        if (line.length() 0)
          g2.drawString(line, (intx, (inty);
        y += fontSize;
      }
    }

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
      if (pageIndex >= pageVector.size())
        return NO_SUCH_PAGE;
      int savedPage = currentPageIndex;
      currentPageIndex = pageIndex;
      Graphics2D g2 = (Graphics2Dg;
      g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      paint(g2);
      currentPageIndex = savedPage;
      return PAGE_EXISTS;
    }

    public Dimension getPreferredSize() {
      return preferredSize;
    }

    public int getCurrentPage() {
      return currentPageIndex;
    }

    public int getNumPages() {
      return pageVector.size();
    }

    public void nextPage() {
      if (currentPageIndex < pageVector.size() 1)
        currentPageIndex++;
      repaint();
    }

    public void previousPage() {
      if (currentPageIndex > 0)
        currentPageIndex--;
      repaint();
    }
  }  
}

           
       
Related examples in the same category
1. Print an Image to print directly
2. Simplest SWT Print ExampleSimplest SWT Print Example
3. Print in Java 2: PrinterJob
4. Print in Java: page format and document
5. Print in Java: Multi page
6. Print in Java 5
7. Print in Java 6
8. Simple Book for printingSimple Book for printing
9. Shapes PrintShapes Print
10. Display the print dialog and print
11. Print the printable area outlinePrint the printable area outline
12. Printable demoPrintable demo
13. Print Swing componentsPrint Swing components
14. BookBook
15. Another print demoAnother print demo
16. Book demoBook demo
17. Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
18. Printing the Java 1.4 Way
19. Prompting for a Printer
20. Printing the Java 1.1 WayPrinting the Java 1.1 Way
21. ScribbleScribble
22. Printable Document
23. PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
24. Print to the standard output
25. PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
26. Print Demo: BookPrint Demo: Book
27. Print Test Print Test
28. Pageable TextPageable Text
29. Printable Component
w_ww.j___a___v_a2_s___.___com_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.