Class XHTMLPanel

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, FSCanvas, UserInterface, FormSubmissionListener, RepaintListener
Direct Known Subclasses:
ScalableXHTMLPanel

@ParametersAreNonnullByDefault public class XHTMLPanel extends BasicPanel

XHTMLPanel is a simple Swing component that renders valid XHTML content in a Java program. It is scrolling aware so you can safely drop it into a JScrollPane. The most common usage is to stuff a URL into it and then add it to your JFrame. Ex:


 import org.xhtmlrenderer.simple.*;
 
 public static void main(String[] args) {
 
 // set up the xhtml panel XHTMLPanel xhtml = new XHTMLPanel();
 xhtml.setDocument(new URL("http://myserver.com/page.xhtml"));
 
 JScrollPane scroll = new JScrollPane(xhtml);
 JFrame frame = new JFrame("Demo");
 frame.getContentPane().add(scroll);
 frame.pack();
 frame.setSize(500,600);
 frame.show();
 }
 

XHTMLPanel renders XHTML and XML which can be loaded as valid Document instances. You should make sure the document you want to render is well-formed. For XHTML, there is always a default stylesheet available, even if no CSS is attached to the XHTML you are loading. For XML, there is no default stylesheet, so you should have one attached to your XML before trying to render it using the xml-stylesheet processing instruction. XHTMLPanel has methods to load documents from a uri (setDocument(String uri)), from a Document instance (setDocument(Document)) or from an InputStream (BasicPanel.setDocument(java.io.InputStream,String)).

XHTMLPanel also lets you make simple changes with simple methods like setFontScalingFactor(float). If you want to make other changes you will need to get the rendering context (RootPanel.getSharedContext()) and call methods on that. Ex: </p> @{code

}

 XHTMLPanel xhtml = new XHTMLPanel();
 RenderingContext ctx = xhtml.getRenderingContext();
 ctx.setLogging(true); // turn on logging
 ctx.setValidating(true); // turn on doctype validation
 ctx.addFont(fnt,"Arial"); // redefine a font
 ctx.setDomImplementation("com.cooldom.DomImpl");
 

XHTMLPanel comes with a pre-installed MouseListener which handles :hover events used for rollovers ( @see org.xhtmlrenderer.swing.HoverListener ). XHTMLPanel also comes with a pre-installed LinkListener used to follow links. ( @see org.xhtmlrenderer.swing.LinkListener ) If you want to disable these for some reason you can get the list of mouse listeners and remove them all.

See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • fontScalingFactor

      private float fontScalingFactor
    • minFontScale

      private float minFontScale
    • maxFontScale

      private float maxFontScale
  • Constructor Details

    • XHTMLPanel

      public XHTMLPanel()
      Instantiates an XHTMLPanel with no Document loaded by default.
    • XHTMLPanel

      public XHTMLPanel(UserAgentCallback uac)
      Instantiates a panel with a custom UserAgentCallback implementation.
      Parameters:
      uac - The custom UserAgentCallback implementation.
  • Method Details

    • setupListeners

      private void setupListeners()
    • resetListeners

      private void resetListeners()
    • relayout

      public void relayout()
      Lays out the current document again, and re-renders.
      Overrides:
      relayout in class RootPanel
    • setDocument

      public void setDocument(String uri)
      Loads and renders a Document given an uri. The uri is resolved by the UserAgentCallback
      Overrides:
      setDocument in class BasicPanel
    • setDocument

      public void setDocument(Document doc)
      Renders an XML Document instance. Make sure that no relative resources are needed
      Parameters:
      doc - The document to render.
    • setDocument

      public void setDocument(Document doc, @Nullable String url)
      Renders a Document using a URL as a base URL for relative paths.
      Overrides:
      setDocument in class BasicPanel
      Parameters:
      doc - The new document value
      url - The new document value
    • setDocument

      public void setDocument(InputStream stream, String url)
      Renders a Document read from an InputStream using a URL as a base URL for relative paths.
      Overrides:
      setDocument in class BasicPanel
      Parameters:
      stream - The stream to read the Document from.
      url - The URL used to resolve relative path references.
    • setDocument

      public void setDocument(File file) throws MalformedURLException
      Renders a Document read from an InputStream using a URL as a base URL for relative paths.
      Parameters:
      file - The file to read the Document from. Relative paths will be resolved based on the file's parent directory.
      Throws:
      MalformedURLException
    • setSharedContext

      public void setSharedContext(SharedContext ctx)
      Sets the RenderingContext attribute of the XHTMLPanel object. Generally you should not use this unless you have a heavily customized context to use. To modify just some rendering behavior, consider using RootPanel.getSharedContext() to retrieve the current context, and using mutators to change its behavior.
      Overrides:
      setSharedContext in class BasicPanel
      Parameters:
      ctx - A new RenderingContext to use for rendering.
    • setFontScalingFactor

      public void setFontScalingFactor(float scaling)
      Sets the scaling factor used by incrementFontSize() and decrementFontSize()--both scale the font up or down by this scaling factor. The scaling roughly modifies the font size as a multiplier or divisor. A scaling factor of 1.2 applied against a font size of 10pt results in a scaled font of 12pt. The default scaling factor is 1.2F.
    • incrementFontSize

      public void incrementFontSize()
      Increments all rendered fonts on the current document by the current scaling factor for the panel. Scaling applies cumulatively, which means that multiple calls to this method scale fonts larger and larger by applying the current scaling factor against itself. You can modify the scaling factor by setFontScalingFactor(float), and reset to the document's specified font size with resetFontSize().
    • resetFontSize

      public void resetFontSize()
      Resets all rendered fonts on the current document to the font size specified in the document's styling instructions.
    • decrementFontSize

      public void decrementFontSize()
      Decrements all rendered fonts on the current document by the current scaling factor for the panel. Scaling applies cumulatively, which means that multiple calls to this method scale fonts smaller and smaller by applying the current scaling factor against itself. You can modify the scaling factor by setFontScalingFactor(float), and reset to the document's specified font size with resetFontSize().
    • scaleFont

      private void scaleFont(float scaleBy)
      Applies a change in scale for fonts using the rendering context's text renderer.
    • getMaxFontScale

      public float getMaxFontScale()
      Returns the maximum font scaling that may be applied, e.g. 3 times assigned font size.
    • getMinFontScale

      public float getMinFontScale()
      Returns the minimum font scaling that may be applied, e.g. 0.5 times assigned font size.
    • setMaxFontScale

      public void setMaxFontScale(float f)
      Sets the maximum font scaling that may be applied, e.g. 3 times assigned font size. Calling incrementFontSize() after this scale has been reached doesn't have an effect.
    • setMinFontScale

      public void setMinFontScale(float f)
      Sets the minimum font scaling that may be applied, e.g. 3 times assigned font size. Calling decrementFontSize() after this scale has been reached doesn't have an effect.