Controlling the print dialog box type - Java 2D Graphics

Java examples for 2D Graphics:Print

Description

Controlling the print dialog box type

Demo Code

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
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;

public class Main extends JFrame {
    //  w  w w. jav a  2  s.c  o m
    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(event-> {
                final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
                attributes.add(DialogTypeSelection.COMMON);
                PrinterJob printJob = PrinterJob.getPrinterJob();
                printJob.printDialog(attributes);
        });
        this.add(printDialogButton);
        
        JButton exitButton = new JButton("Exit");    
        exitButton.addActionListener(e-> {
                System.exit(0);
        });
        this.add(exitButton);
    }
    public static void main(String[] args) {           
        SwingUtilities.invokeLater(()->{
                Main window = new Main();
                window.setVisible(true);
        });

    }
}

Related Tutorials