import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

public class printbutton extends JPanel implements ActionListener {

    public printbutton() {
	setBackground(Color.white);
        JButton b = new MyButton();
        b.addActionListener(this);
        add(b);

    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("action");
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable((MyButton) e.getSource());

/*
//Page dialog
        PageFormat pf = printJob.pageDialog(printJob.defaultPage());
*/

/*
//Print dialog
	if(printJob.printDialog()){
          try { printJob.print(); } catch (Exception PrintException) { }
    	}
*/

//No dialogs
          try { printJob.print(); } catch (Exception PrintException) { }

    }

    public static void main(String s[]) {
	WindowListener l = new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {System.exit(0);}
	};
	Frame f = new Frame("printbutton");
	f.addWindowListener(l);
	f.add("Center", new printbutton());
	f.pack();
	f.setSize(new Dimension(400,300));
	f.show();
    }

    class MyButton extends JButton implements Printable {

        public MyButton() {
            super("MyButton");

        }
	
        public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
		if (pi >= 1) {
                return Printable.NO_SUCH_PAGE;
		}

            System.out.println("mybutton print");
            Graphics2D g2 = (Graphics2D) g;
      	    g2.translate(pf.getImageableX(), pf.getImageableY());
            Font  f = new Font("Monospaced",Font.PLAIN,12);
	    System.out.println (f);
	    g2.setFont (f);
	    paint (g2);
            return Printable.PAGE_EXISTS;
        }
    }
}
