PrintTestApp.java Source code

Java tutorial

Introduction

Here is the source code for PrintTestApp.java

Source

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Properties;

import javax.swing.JFrame;

public class PrintTestApp extends JFrame {
    TextArea textArea = new TextArea();

    Toolkit toolkit;

    public static void main(String args[]) {
        PrintTestApp app = new PrintTestApp();
    }

    public PrintTestApp() {
        super("PrintTestApp");
        toolkit = getToolkit();
        add("Center", textArea);
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        String name = "Test print job";
        Properties properties = new Properties();
        PrintJob pj = toolkit.getPrintJob(PrintTestApp.this, name, properties);
        if (pj == null)
            textArea.setText("A null PrintJob was returned.");
        else {
            String output = "Name: " + name + "\nProperties: " + properties.toString();
            Dimension pageDim = pj.getPageDimension();
            int resolution = pj.getPageResolution();
            boolean lastPageFirst = pj.lastPageFirst();
            output += "\nPage dimension (in pixels):";
            output += "\n height: " + String.valueOf(pageDim.height);
            output += "\n width: " + String.valueOf(pageDim.width);
            output += "\nResolution (pixels/inch): " + String.valueOf(resolution);
            output += "\nLast Page First: " + String.valueOf(lastPageFirst);
            textArea.setText(output);
            Graphics g = pj.getGraphics();
            g.dispose();
            pj.end();
        }
    }

    public void itemStateChanged(ItemEvent e) {
    }

}