jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Printable Class print method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printable Class print method question" Watch "Printable Class print method question" New topic
Author

Printable Class print method question

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I guess I am missing something basic using the 'Printable' class. I am having trouble getting the right number of pages output. After inserting some debug statements to trace my logic it seems that the 'print' method is getting called AFTER I return 'NO_SUCH_PAGE'. Is that supposed to happen and if so why and how do I stop it? If not then please confirm so I can look for an unwanted extra use of the class somewhere. TIA.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8052
    
  22

Dennis Putnam wrote:I guess I am missing something basic using the 'Printable' class.

What class? Printable is an interface.

As for the rest, there really is very little we can do without some code. Please supply an SSCCE (and please read the link first).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the reply. I meant that I have a class that implements 'Printable', sorry. What I was trying to confirm is whether or not it is expected that the 'print' method is invoked a second time after returning 'NO_SUCH_PAGE'. I take it from your reply that is it not and 'print' should not be called again, so I need to try and find how my 'PrinterJob' is causing this behavior. If I get stuck I will post the relevant code in the hope someone will spot something without necessarily needing to run it as posting an SSCCE would not be practical given all the ancillary code needed to create the environment causing this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Yes, I think you are missing something basic. If you look at the API documentation for Printable, there's a set of bullet points. The first of them starts out:

The printing system may request a page index more than once.


So if your question is "Is that supposed to happen?" then the answer is clearly "Yes".
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
OK, I need to reread the docs and try to see what I am not understanding.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Sorry, I'm lost. What's this "index page" you are asking about?
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I hit the send key before I realized I was misreading what you wrote. I guess my dyslexia saw indexpage rather than page index. In any case I must not be looking at the right docs as I don't see what you are talking about.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

http://docs.oracle.com/javase/7/docs/api/java/awt/print/Printable.html
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks but I wonder why that page is different than the one I was using since both are from Oracle? Anyway, that really complicates my logic if I cannot output pages sequentially. However, I understand the reason behind it except requesting the same page twice but I can deal with it as long as I know it can happen. One thing that was not clear to me is am I correct that it will never request a page index less than the highest previous ones or it can but I am supposed to ignore it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Dennis Putnam wrote:Thanks but I wonder why that page is different than the one I was using since both are from Oracle?


The page you linked to was the documentation for Java 4. My page was the current documentation (Java 7).

Anyway, that really complicates my logic if I cannot output pages sequentially. However, I understand the reason behind it except requesting the same page twice but I can deal with it as long as I know it can happen. One thing that was not clear to me is am I correct that it will never request a page index less than the highest previous ones or it can but I am supposed to ignore it?


If you're asking me to interpret all of that legalistic waffling that pretends to be documentation, sorry. You're just going to have to deal with it as best you can.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Paul Clapham wrote:
The page you linked to was the documentation for Java 4. My page was the current documentation (Java 7).

Damn search engines.
Paul Clapham wrote:
If you're asking me to interpret all of that legalistic waffling that pretends to be documentation, sorry. You're just going to have to deal with it as best you can
.

No. What I was hoping is if I interpreted this one run-on sentence correctly:

With the exception of re-imaging an entire document for multiple collated copies, the increasing page index order means that when page N is requested if a client needs to calculate page break position, it may safely discard any state related to pages < N, and make current that for page N. "State" usually is just the calculated position in the document that corresponds to the start of the page.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

If you're waiting for somebody to give you a definitive certification on your interpretation of that sentence, you're going to have to wait a bit longer. I haven't worked with that interface for several years so I really don't remember whether that was an issue or not, or how it actually worked.

In the meantime I would suggest you should step up and become the expert on this topic. You've already got a fair amount of experience with it and you should be able to sort things out with a bit of testing. Let us know especially if you find that specification being violated.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I don't know about the expert part but I have this silly thing figured out and working. This interface cannot be accused of making printing simple and the documentation adds to the confusion, IMO. It is a page-centric implementation which means you need to change how you think about printing. However, since it is meant to interface with a print dialog I guess it makes sense since one can specify selected pages and ranges. The hardest part for me was thinking in terms of pages rather than lines. One must know ahead of time what fits on a particular page. In my case I was trying to output an arraylist of object data with the various elements of each object as a row. The crux of the problem is that the 'print' method can request any page at any time including repeating pages (don't know why but it seems requesting the first (0th index) page twice is always the case) even though they print only once. Thus on any invocation you need to calculate what goes on the requested page, meaning you need to need to have the complete document prepared, to some extent, rather than simply generating sequential lines. Fortunately my arraylist is available throughout the app so all I needed to do was calculate the start and stop indices as a function of the page index. My header and footer was consistent so they did not pose a problem. There is another gotcha that I still need to deal with and that is portrait vs. landscape orientation. I currently only have it working for portrait but supporting landscape opens another can of worms as all the pagination calculations would need to be redone. It seems that if one's app is generating sequential lines for printing, it will be necessary to create your own document class into which you can put the lines as they are generated by page like conventional printing. In other words create the entire document before printing it. Then write the 'print' method to essentially randomly select pages from that document class returning the appropriate value depending on whether or not the page exists. For large reports I can see easily using up a lot of memory. It may be necessary to write such a document to some intermediate storage media then read it back within the 'print' method.

One note: I was not able to come up with a condition where it requested a lower page index than any previous page index. However, my logic now does not care what page is requested so if it happens my code will handle it.

Edit: Upon further experimenting, at least in my case, it requests every page twice not just the first one. The thinking on that is a head scratcher.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Congratulations!

You seem to have come up with something similar to what I did, which was to produce a paginated version of the data structure in memory. As you observe, that makes it easy to return any page in the printed version on demand, but it can be a major memory hog if the output is very large.

As for portrait versus landscape (or different-size fonts, or anything else which can change the pagination of the output), that needs to be an attribute of whatever you wrote which implements Printable, and then it needs to be integrated into the pagination calculations.
 
Don't get me started about those stupid light bulbs.
 
subject: Printable Class print method question