This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes printing an ArrayList contents to printer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "printing an ArrayList contents to printer" Watch "printing an ArrayList contents to printer" New topic
Author

printing an ArrayList contents to printer

evan theon
Ranch Hand

Joined: Feb 16, 2005
Posts: 39
i have just learned how to print a text ( not a text file ) in Java, and i also have just learned how to make a program with GUI using AWT and SWING ( i understand only a little bit of those two techniques, just adequate for me to make my program works ).
now what i'm trying to do is to make a GUI program that prints an ArrayList's contents when i push a 'PRINT' button.
the bottomline of my case is that i need to know how to make a print method with an ArrayList parameter which contents is going to be print.
let me simplify more :
say i have this program i used to print
import java.awt.*;

import java.awt.font.*;

import java.awt.geom.*;

import java.awt.print.*;





public class PrintInvoice {



private final static int POINTS_PER_CM = 29;



public PrintInvoice () {



PrinterJob printJob = PrinterJob.getPrinterJob ();



Book book = new Book ();



PageFormat documentPageFormat = new PageFormat ();

documentPageFormat = printJob.pageDialog(documentPageFormat);

for(int j=0; j<10; j++)

book.append (new IntroPage (), documentPageFormat);





printJob.setPageable (book);



if (printJob.printDialog()) {

try {

printJob.print();

} catch (Exception PrintException) {

PrintException.printStackTrace();

}

}



}



static public void main(String args[])

{

PrintInvoice ex = new PrintInvoice();

}



private class IntroPage implements Printable {





public int print (Graphics g, PageFormat pageFormat, int page) {



Graphics2D g2d = (Graphics2D) g;



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



g2d.setPaint (Color.black);



Font titleFont = new Font ("Times New Roman", Font.BOLD, 14);

g2d.setFont (titleFont);

g2d.drawString ("text1", (int)(17.5*POINTS_PER_CM), (int)(2*POINTS_PER_CM));



titleFont = new Font ("Times New Roman", Font.PLAIN, 11);

g2d.setFont (titleFont);



g2d.drawString ("text2", (int)(2.5*POINTS_PER_CM), (int)(2.6*POINTS_PER_CM));

g2d.drawString ("text3", (int)(2.5*POINTS_PER_CM), (int)(3.1*POINTS_PER_CM));



titleFont = new Font ("Times New Roman", Font.PLAIN, 10);

g2d.setFont (titleFont);

for (int i = (6*POINTS_PER_CM); i < (int)(11.5*POINTS_PER_CM); i += 14) {

g2d.drawString ("texta", (int)(1*POINTS_PER_CM), i);

g2d.drawString ("textb", (int)(1.9*POINTS_PER_CM), i);

g2d.drawString ("textc", (int)(2.9*POINTS_PER_CM), i);

}

titleFont = new Font ("Times New Roman", Font.PLAIN, 11);

g2d.setFont (titleFont);

g2d.drawString ("text4", (int)(18.5*POINTS_PER_CM), (int)(12.2*POINTS_PER_CM));

g2d.drawString ("text5", (int)(18.5*POINTS_PER_CM), (int)(12.7*POINTS_PER_CM));

g2d.drawString ("text6", (int)(18.5*POINTS_PER_CM), (int)(13.2*POINTS_PER_CM));



return (PAGE_EXISTS);

}

}



}

you will see that i'm printing "text1","text2",etc in a certain position, now i want to replace those texts with caontents from an ArrayList. can somebody give me some hints,codes,links,examples,explanations,or anything that might help?
any help would be great thank you very much
Hrishikesh Ghatnekar
Greenhorn

Joined: Oct 25, 2004
Posts: 28
g2d.drawString ("text4", (int)(18.5*POINTS_PER_CM), (int)(12.2*POINTS_PER_CM));

Instead of text4 your can use arrayobject.toString();
Which will give you [ array contains, array contains,....] result

Else extend a class from arraylist , and override toString method to get the desired output.

Thanks
evan theon
Ranch Hand

Joined: Feb 16, 2005
Posts: 39
oops...i forgot to mentioned that my problem is how to pass the ArrayList object to my print method i used ( which i have wrote on my last posting ) once i can get the Arraylist read inside my print method, the next step step would be more simple.
i've tried to do this in my sub class' constructor :

private class IntroPage(ArrayList) implements Printable

as you can see in my coding (on the last posting) i use this IntroPage class to do the printing job and design the result. but when i change its declaration (adding a parameter to it) it doesn't work!
the error message was that i need to add a "("
which i assumed i already did. is there any rules on printing in Java that i missed??

thanks for your help i really appreciate it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printing an ArrayList contents to printer