Print configuration : PrintJob « 2D Graphics « Java Tutorial






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) {
  }

}








16.49.PrintJob
16.49.1.Print with java.awt.PrintJob
16.49.2.Print Sample with java.awt.GraphicsPrint Sample with java.awt.Graphics
16.49.3.Print configuration
16.49.4.Listening for Print Job Attribute Changes
16.49.5.Determining When a Print Job Has Finished
16.49.6.Cancelling a Print Job
16.49.7.Getting the Default Value of a Print Job Capability
16.49.8.Getting the Possible Values for a Print Job Capability
16.49.9.Setting the Orientation of a Print Job -- Portrait or Landscape
16.49.10.demonstrate the Java 1.4 printing API
16.49.11.Setting the Number of Copies of a Print Job
16.49.12.Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
16.49.13.Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
16.49.14.Setting the Orientation of a Printed Page