import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class Printout extends JFrame implements ActionListener { public Printout() { super("print this frame"); //Place a table and some buttons in th frame. JTable jt = new JTable(new String[][] { {"This", "is"}, {"a", "Test"} }, new String[] {"Column 1", "Column 2"}); JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JButton("knapp 1")); p.add(new JButton("knapp 2")); JButton b = new JButton("print"); b.addActionListener(this); p.add(b); getContentPane().add(p, BorderLayout.SOUTH); //Show the frame. setSize(300, 200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } public void actionPerformed(ActionEvent e) { //Get the default toolkit. Toolkit tk = Toolkit.getDefaultToolkit(); //Create a Properties object and a some property (not nessecary). Properties theProps = new Properties(); theProps.put("awt.print.numCopies", "3"); //Get a print job. PrintJob pj = tk.getPrintJob(this, "printing", theProps); if (pj != null) { //Get the print graphics. Graphics g = pj.getGraphics(); System.out.print("user printed, theProps = "); theProps.list(System.out); System.out.println(); //Start printing. printAll(g); paint(g); //Flush print stream. g.dispose(); pj.end(); } else System.out.println("user did not print"); } public static void main(String args[]) { new Printout(); } }