I've been reading those forums for a while, but now I need some help I can't find anything on, so here goes:
I'm trying to print an array of Swing components (JTextPanes in particular). When I call for each component it works without any trouble. However I don't want the end user to have and confirm the printing of each component (it's a rather large array). And here comes the problem, when I omit calling the printDialog before each component the printing gets all messy (some leading white areas, re-sized). Anyone has any idea what this might be due to and jow can I get it to work?
What you could try is the following:
- retrieve the PrinterJob as usual
- use the printDialog method that takes a PrintRequestAttributeSet reference
- retrieve the PrintService from the PrinterJob - call the print method that takes a PrintService on the text components instead of the setPrintable and print methods on the PrinterJob. Pass the PrintService and PrintRequestAttributes you used in the previous steps
I might be missing something here, but TextComponent (or JTextComponent for that matter) only has a print method that takes Graphics, and none for PrintService or PrintRequestAttributes.
Furthermore when I call the printDialog method of the PrinterJob with PrintRequestAttributes, even when I call it for each JTextPane it still gets messy. It only seems to work for me when calling the native printDialog.
Hristo Piyankov wrote:I might be missing something here, but TextComponent (or JTextComponent for that matter) only has a print method that takes Graphics, and none for PrintService or PrintRequestAttributes.
Is it me, or is there a confusion with between the print and paint?
Hello everyone and thanks for the help and patience. Unfortunately, things are getting really messy (or maybe I'm too tired). Now part of the code, I could swear was pretty functional until today is acting funny again. Here is a bit more detail:
I have an array of JTextPanes which I need to print. The JTextPanes themselves have a JTable as component. Each JTextPane in the array is unique and so is each JTable in the panes. Currently I have been using the method I described (having the user to confirm each print) and it was working quite ok (until today).
The code I am using is the following:
Button for printing the list...
And then the printUtilites to print it (which I shamelessly borrowed form google ;))...
Now as well as this was performing yesterday, currently it yields the following results:
- on the first press of the "Print" button, it prints the first JTextPane just as it is supposed to be. Then it prints all the rest pages as blank.
- on the second press of the "Print" button , it prints the first JTextPane just as it is supposed to be. Then it prints all the rest pages with the JTable missing.
In regards to Rob Prime's suggestion:
Yes it was my bad, I really need to learn to read . But, when I do it with the method you suggested, I get the contents of the page shrinked (huge indents form all sides) and as usual my JTable is missing.
I'd really appreciate any feedback as I feel totally lost here of what the reason for this strange behavior might be.
Joined: Sep 12, 2009
Got it all fixed now. Thanks everyone for their cooperation, it really helped me clear some things up.
Could you please tell us how you fixed it so future users with a similar problem can benefit from it.
Joined: Sep 12, 2009
Well the problem was rather multi-sided (not only in the printerJob).
The problem with my JTable not displaying when printing, was that the JTextArea was not sized/made displayable (or at least that is what I think it was). Adding it to a JFrame and calling pack() solved this.
About the printing: after fixing the previous problem I was able to print the entire List with a single printJob, calling a single printDialog, then calling setPrintable on each component and then printing it.