I am a relatively new programmer so this may be a really simple question to answer but its got me a bit stumped..
I\'m trying to print my Java GUI\'s final output t
instead of generating a Doc you could print your graphics of a JFrame/JPanel directly. This code should work:
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName("name");
PageFormat format = pj.getPageFormat(null);
pj.setPrintable (new Printable() {
@Override
public int print(Graphics pg, PageFormat pf, int pageNum) throws PrinterException {
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
this.paint(g2);
return Printable.PAGE_EXISTS;
}
}, format);
if (pj.printDialog() == false)
return;
pj.print();
} catch (PrinterException ex) {
// handle exception
}
}
So I found a way that works perfectly for my situation and I thought I would just post what it was in case it would be useful to anyone.
The basics of the solution are that Java does have its own full fledged (At least compared to mine) printDialog popUp that has more than I needed (Page layout editing, previews, etc) and all you have to do to use it is hand it an object that implements Printable and it is within that object that you create a graphic and draw your document.
I just needed to draw my output String and that was easily done, I even found a StringReader so I can stop naively Writing a File just to get my output in a BufferedReader.
Here's the code. There are two parts, the method and the class where I draw the image:
Method:
private void printToPrinter()
{
String printData = CalculationTextArea.getText() + "\n" + SpecificTextArea.getText();
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new OutputPrinter(printData));
boolean doPrint = job.printDialog();
if (doPrint)
{
try
{
job.print();
}
catch (PrinterException e)
{
// Print job did not complete.
}
}
}
And here is the Class where the document is printed:
public class OutputPrinter implements Printable
{
private String printData;
public OutputPrinter(String printDataIn)
{
this.printData = printDataIn;
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
// Should only have one page, and page # is zero-based.
if (page > 0)
{
return NO_SUCH_PAGE;
}
// Adding the "Imageable" to the x and y puts the margins on the page.
// To make it safe for printing.
Graphics2D g2d = (Graphics2D)g;
int x = (int) pf.getImageableX();
int y = (int) pf.getImageableY();
g2d.translate(x, y);
// Calculate the line height
Font font = new Font("Serif", Font.PLAIN, 10);
FontMetrics metrics = g.getFontMetrics(font);
int lineHeight = metrics.getHeight();
BufferedReader br = new BufferedReader(new StringReader(printData));
// Draw the page:
try
{
String line;
// Just a safety net in case no margin was added.
x += 50;
y += 50;
while ((line = br.readLine()) != null)
{
y += lineHeight;
g2d.drawString(line, x, y);
}
}
catch (IOException e)
{
//
}
return PAGE_EXISTS;
}
}
Anyways that is how I solved this problem! Hope it can be of some use to someone!
Create a JTextComponent
(I suggest a JTextArea
so you can use append()
), and append what you need to the field. Do not display it on the view, it is simply a hidden field for printing purposes.
All JTextComponent
s have a print()
method. Simply call hiddenTextArea.print()
and the rest is handled for you.
JTextArea hiddenTextArea = new JTextArea();
for (String s : dataToPrintCollection) {
hiddenTextArea.append(s + "\n");
}
try {
hiddenTextArea.print();
} catch (PrinterException e) {}