Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Printing an array of Swing components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Printing an array of Swing components" Watch "Printing an array of Swing components" New topic
Author

Printing an array of Swing components

Hristo Piyankov
Greenhorn

Joined: Sep 12, 2009
Posts: 6
Hello everyone,

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?

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hristo Piyankov
Greenhorn

Joined: Sep 12, 2009
Posts: 6
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

What java version are you on?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Hristo Piyankov
Greenhorn

Joined: Sep 12, 2009
Posts: 6
1.6.0_11
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

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?

From the API docs of JTextComponent


Isn't this the one Rob was talking about?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

It surely was.
Hristo Piyankov
Greenhorn

Joined: Sep 12, 2009
Posts: 6
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.
Hristo Piyankov
Greenhorn

Joined: Sep 12, 2009
Posts: 6
Got it all fixed now. Thanks everyone for their cooperation, it really helped me clear some things up.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Well done
Could you please tell us how you fixed it so future users with a similar problem can benefit from it.
Hristo Piyankov
Greenhorn

Joined: Sep 12, 2009
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing an array of Swing components