File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT / JFace and the fly likes Printing a swing component in Java (cutting it into multipla pages) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Printing a swing component in Java (cutting it into multipla pages)" Watch "Printing a swing component in Java (cutting it into multipla pages)" New topic
Author

Printing a swing component in Java (cutting it into multipla pages)

Andrej Mihovec
Greenhorn

Joined: Nov 08, 2010
Posts: 2
Hi all,
I have a project written in Java.
Recently I had to upgrade it to print the "forms".
The "forms" are basicly JPanels made of JTextFields, JTables, JLabels and other swing components.
Now, the easiest way for me to implement the printing would be to just print the swing component (JPanel) like it is.

BUT, there are couple of problems I've come across that I don't know how to solve...

1. here's my print code


now this code prints the component like it is, BUT if the componennt is bigger then the paper size, there is no automatic split into other pages.. there's just this one page and that's it...
So I'm trying to implement a solution to maybe make a method that prints the 1st part of the page, and than for the second, I offset the Y coordinate and make a "picture" of the second page..
The only problem is I don't know how to implement it... Maybe my whole idea is wrong and there is another easier way to solve this...
Any tips, hints or help are welcome!!!
Oh, and thank you for your time!!
Andrej
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

Howdy, Andrej. Welcome to our JavaRanch!

Let's slide this over to the Swing / AWT / SWT / JFace forum, where I think this question fits better!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Andrej Mihovec
Greenhorn

Joined: Nov 08, 2010
Posts: 2
Oh ok.. sorry for this.. have the admins move it please...

and for the record, the solution for my problem is ->

implement Printable and Pageable in the class you wish to print and have Prinable return the desired number of pages...
also through Printable I was able to make some components invisible during print, and make the background white...
the key is to offset the Y coordinate for each page!

preety simlpe really...
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

I'm glad you made it, champ!
Arhyal gyel
Greenhorn

Joined: Feb 03, 2012
Posts: 5
Hello greenHorn, i joined java ranch mainly because i wanted to ask you to help me out with the code you said you rectified to be able to print multiple pages of a swing component.
I implemented the printable interface and was able to scrap up a little paging using the Book class, but my code either prints the same page multiple times (without scrolling) , or it prints multiple pages with overlaps and the shifts the component i want to print to the far right, so much that it moves out of the printing area.
I'dd appreciate it if you could upload a sample of your code here. thanks a lot.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

Greetings, Arhyal. Welcome to JavaRanch.

Ideally, you could have started a new thread, but since this one isn't that old, we'll just leave your post here.

Now, please, UseTheForumNotEmail. Let's say that, in the future, someone has the exact same question as you, but the answer was sent to your email by someone. This means that this other person that has the same doubt as you would be loosing a chance to learn new things, and we don't want that, right? So, I edited your original post and removed your email.
Arhyal gyel
Greenhorn

Joined: Feb 03, 2012
Posts: 5
Ok, fine.
 
 
subject: Printing a swing component in Java (cutting it into multipla pages)
 
Threads others viewed
how to print entire Jframe ?
printdialog properties..
Some Help will do good !!!
printing of scrorableresultsetable.
about printing
developer file tools