printing in java

hey guys, I want to make a class that sends all the contests of a frame to a printer. Am currently using the code below but it only prints the visible contents. how can i modify it?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.print.*;

public class Printer implements Printable, ActionListener {

//JFrame frameToPrint;

JScrollPane pane;

//public Printer(JFrame f) {

public Printer(JScrollPane p) {

pane = p;

}

public int print(Graphics g, PageFormat pf, int page) throws

PrinterException {

if (page > 0) { /* We have only one page, and 'page' is zero-based */

return NO_SUCH_PAGE;

}

/* User (0,0) is typically outside the imageable area, so we must

* translate by the X and Y values in the PageFormat to avoid clipping

*/

Graphics2D g2d = (Graphics2D)g;

g2d.translate(pf.getImageableX(), pf.getImageableY());

/* Now print the window and its visible contents */

//frameToPrint.printAll(g);

pane.printComponents(g);

/* tell the caller that this page is part of the printed document */

return PAGE_EXISTS;

}

public void actionPerformed(ActionEvent e) {

PrinterJob job = PrinterJob.getPrinterJob();

job.setPrintable(this);

boolean ok = job.printDialog();

if (ok) {

try {

job.print();

} catch (PrinterException ex) {

/* The job did not successfully complete */

}

}

}

public static void main(String args[]) {

UIManager.put("swing.boldMetal", Boolean.FALSE);

JFrame f = new JFrame("Print UI Example");

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}

});

JTextArea text = new JTextArea(50, 20);

for (int i=1;i<=100;i++) {

text.append("Line " + i + "\n");

}

JScrollPane pane = new JScrollPane(text);

pane.setPreferredSize(new Dimension(250,200));

f.add("Center", pane);

JButton printButton = new JButton("Print This Window");

printButton.addActionListener(new Printer(pane));

f.add("South", printButton);

f.pack();

f.setVisible(true);

}

}

[2287 byte] By [musiigea] at [2007-11-27 10:13:59]
# 1

Printing is a bit of a nightmare. To use this approach you'll need to create a purpose-built instance of the frame with an appropriately altered layout.

Otherwise WYSIWYG.

malcolmmca at 2007-7-28 15:30:25 > top of Java-index,Java Essentials,Java Programming...
# 2

which solution would you recommend? and how can it be implemented? thanks alot

musiigea at 2007-7-28 15:30:25 > top of Java-index,Java Essentials,Java Programming...
# 3

Probably depends on how many different forms you are likely to wind up printing.

Personally I wound up writing my own print library, calling the basic one but with special layout facilities.

Tables, in particular, are a hastle. The built in table printer is very limited if you wnat to print stuff in addition to the table.

malcolmmca at 2007-7-28 15:30:25 > top of Java-index,Java Essentials,Java Programming...
# 4

Man, am totally green about this topic. The class am using, I got it from suns tutorial. Do u know where I can find a tutorial about that?

Basically what i want to print all the data in my scroll application. Right now i can only print what is visible in the application?

musiigea at 2007-7-28 15:30:25 > top of Java-index,Java Essentials,Java Programming...
# 5

There's a simple print facility build into JTable. That prints the whole table with a simple page header/footer.

Getting a reasonably reliable printing framework into my program took me weeks. The central problem, completely unaddressed by the java.awt.print stuff, is page layout. If you look in the java library source code, there's a package local class called "PrintTable" which is what does the printing if you use the getPrintable() methods of JTable. This is probably a good place to get the idea of what may be required.

For composite print outs you either have to set up your layout "by hand" or explicity call the layout methods before calling the print() method on the container.

You are likely to have to do a great deal of transforming and clipping, for example printing a page from a table you need to shift the origin and clip to get the block you want.

malcolmmca at 2007-7-28 15:30:25 > top of Java-index,Java Essentials,Java Programming...