private void launchGUI() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a JPanel subclass to render the page
final XHTMLPanel panel = new XHTMLPanel();
final Dimension orgSize = panel.getSize();
// we'll add a listener to note the preferred size as calculated by the render engine
panel.addDocumentListener(new DefaultDocumentListener() {
public void documentLoaded() {
frame.setTitle("Flying Saucer: " + panel.getDocumentTitle());
final Dimension dim = panel.getPreferredSize();
XRLog.general("Preferred size" + dim);
}
});
frame.getContentPane().add(panel);
// Set the XHTML document to render. We use the simplest form
// of the API call, which uses a File reference. There
// are a variety of overloads for setDocument().
try {
panel.setDocument(new File(fileName));
} catch (Exception e) {
XRLog.general(Level.WARNING, "Could not load XHTML document " + fileName, e);
messageAndExit("Failed to load document", -1);
}
// No-op as regards size, but has side-effect of making the window and components "displayable". This is
// necessary for the doDocumentLayout() below to work correctly. Alternately you could setVisible(true), but
// then the document would be visible before the resize was completed.
frame.pack();
// Here we set an artificially large size for the document to let the render engine
// figure out what the document needs based on content.
//
// Note that if the document itself has no fixed width, text will not automatically break and will
// likely run out and extend the document width as far as necessary to accomodate the longest line of text
// you could either set a max width in a document box, or you can set it here.
//
// The document height will be calculated automatically based on content. We use an artificially large size
// here to not constrain the layout algorithm.
panel.setSize(targetWidth, 10000);
panel.doDocumentLayout(panel.getGraphics());
panel.setSize(orgSize);
frame.pack();
frame.setVisible(true);
}