Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package telmo;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.print.*;
- public class Printing implements Printable, ActionListener {
- public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
- Graphics2D graphic = (Graphics2D)g;
- graphic.translate(pf.getImageableX(), pf.getImageableY());
- String[] columnValues = {"a", "b", "c", "d"};
- int width = 0, height = 0;
- for (String l : columnValues) {
- graphic.drawString(l, width, 135 + height);
- width += 15;
- height += graphic.getFont().getSize();
- }
- return PAGE_EXISTS;
- }
- public void actionPerformed(ActionEvent e) {
- PrinterJob job = PrinterJob.getPrinterJob();
- job.setPrintable(this);
- if (job.printDialog())
- try { job.print(); } catch (PrinterException ex) {}
- }
- public static void main(String args[]) {
- UIManager.put("swing.boldMetal", Boolean.FALSE);
- JFrame f = new JFrame("Hello World Printer");
- f.addWindowListener(new WindowAdapter() {
- @Override public void windowClosing(WindowEvent e) {System.exit(0);}
- });
- JButton printButton = new JButton("Print Hello World");
- printButton.addActionListener(new Printing());
- f.add("Center", printButton);
- f.pack();
- f.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement