Note that Flying Saucer is still under intensive development and the way to use it may still change.
However, the simple use of BasicPanel
and XHTMLPanel
should be fairly stable
Please give feedback and help us improve the usability of Flying Saucer
If you just want to show a webpage in your application then start
with the XHTMLPanel
, in org.xhtmlrenderer.swing
. Here is a simple example:
import javax.swing.*; import org.xhtmlrenderer.simple.XHTMLPanel; public class test { public static void main(String[] args) throws Exception { XHTMLPanel panel = new XHTMLPanel(); panel.setDocument("test.xhtml"); JScrollPane scroll = new JScrollPane(panel); JFrame frame = new JFrame(); frame.getContentPane().add(scroll); frame.pack(); frame.setSize(400,400); frame.setVisible(true); } }
First create an XHTMLPanel and set it's preferred size. Call setDocument()
to set the path of
the xhtml file that should be displayed (relative to the current working directory). Then drop it into a
JScrollPane
and add the pane to a JFrame
.
The AboutBox
is a prefab component which displays an html document and automatically
scrolls it. It is primarily useful for Help->About menu items and splash screens.
Here is an example of adding an about box to a menu item's action listener.
import org.xhtmlrenderer.demo.aboutbox.AboutBox; ...... JMenuItem about = new JMenuItem("About..."); about.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { AboutBox ab = new AboutBox("About Flying Saucer", "demos/about/index.xhtml"); ab.setVisible(true); } });
Since network access is outside the scope of the core renderer, form submission
is currently implemented as an ActionListener
attached to the submit button
in the browser application. Depending on your needs you can probably modify
that code. Take a look at org.xhtmlrenderer.demo.browser.SubmitAction.
If you want to access the Swing components for each form input field you
can use the accessors on the Context
object. HTMLPanel.getContext().getFormFieldComponents()
.
Take a look at org.xhtmlrenderer.demo.browser.ResetAction.setupResets()
for
an example of pulling out each reset button and adding a standard AWT ActionListener
to them.
If you want to access the actual DOM elements that make up the form you'll
have to paw through your document tree (start with HTMLPanel.getDocument()
).
By default the XHTMLPanel has a pre-fab mouse listener to follow hyperlinks. You can override this MouseListener
to change it's behavior if you want. See org.xhtmlrenderer.swing.LinkListener
for the complete code.
The core of Flying Saucer is the core-renderer.jar and ss_css2.jar. That's all you need. All of the other jars are only for use by the demo programs.