Java AWT PrinterJob set print dialog box type

Description

Java AWT PrinterJob set print dialog box type

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterJob;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.DialogTypeSelection;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

   public Main() {
      this.setTitle("Example");
      this.setSize(200, 100);
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      this.setLayout(new FlowLayout());

      JColorChooser.showDialog(this, null, Color.blue);
      JButton printDialogButton = new JButton("Print Dialog");
      printDialogButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(DialogTypeSelection.COMMON);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.printDialog(attributes);

         }//ww w  .  java2 s. c  om
      });
      this.add(printDialogButton);

      JButton exitButton = new JButton("Exit");
      exitButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            System.exit(0);
         }
      });
      this.add(exitButton);
   }

   public static void main(String[] args) {
//        JFrame.setDefaultLookAndFeelDecorated(true);

      SwingUtilities.invokeLater(new Runnable() {

         @Override
         public void run() {
            Main window = new Main();
            window.setVisible(true);
         }
      });

   }
}



PreviousNext

Related