JEditorPanes, Preferred Size, and printing HTML

Posted by Ryan Elkins on Stack Overflow See other posts from Stack Overflow or by Ryan Elkins
Published on 2010-04-28T21:04:39Z Indexed on 2010/04/29 0:47 UTC
Read the original article Hit count: 421

Filed under:
|
|
|
|

I'm trying to print some HTML directly, without displaying anything to the user. It works currently (somewhat) using a custom JEditorPane that implements Printable. The problem I'm having is that it always wants to use a preferred size of 582px x 560px. If I manually change the size using something like setSize(x,y) it will change the size of the pane, put the content renders at preferred size, not actual size (so it's still 582x560).

I can scale it up to fit the page, but it's basically just an enlarged version where the images are all pixelated and the layout is wrong (based on the smaller window size).

Inside the print method of my Printable JEditorPane I used this to try and get the size:

    javax.swing.JWindow wnd = new javax.swing.JWindow();
    wnd.setContentPane(this);
    wnd.setSize(1024,1584);
    wnd.pack();
    Dimension d = wnd.getPreferredSize();

With or without that setSize and/or pack methods on the JWindow the preferred size always comes back as 582x560.

I do have control over the html that I'm trying to print but I'd rather not have to rewrite all of that to scale it down so it will print correctly at full size (when scaled up).

© Stack Overflow or respective owner

Related posts about java

Related posts about html