Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Andrej Mihovec
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Andrej Mihovec
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you made it, champ!
 
Arhyal gyel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, fine.
 
Let's get him boys! We'll make him read this tiny ad!
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!