Class XHTMLPanel
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JPanel
-
- org.xhtmlrenderer.swing.RootPanel
-
- org.xhtmlrenderer.swing.BasicPanel
-
- org.xhtmlrenderer.simple.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 aURLinto 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
Documentinstances. 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>@{codeXHTMLPanel 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.
- Author:
- Joshua Marinacci (joshy@joshy.net)
- See Also:
- The Flying Saucer Home Page,
RenderingContext, Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
-
Fields inherited from class org.xhtmlrenderer.swing.BasicPanel
formSubmissionListener
-
Fields inherited from class org.xhtmlrenderer.swing.RootPanel
active_element, doc, focus_element, hovered_element, sharedContext
-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
Constructor Summary
Constructors Constructor Description XHTMLPanel()Instantiates an XHTMLPanel with noDocumentloaded by default.XHTMLPanel(UserAgentCallback uac)Instantiates a panel with a customUserAgentCallbackimplementation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddecrementFontSize()Decrements all rendered fonts on the current document by the current scaling factor for the panel.floatgetMaxFontScale()Returns the maximum font scaling that may be applied, e.g.floatgetMinFontScale()Returns the minimum font scaling that may be applied, e.g.voidincrementFontSize()Increments all rendered fonts on the current document by the current scaling factor for the panel.voidrelayout()Lays out the current document again, and re-renders.voidresetFontSize()Resets all rendered fonts on the current document to the font size specified in the document's styling instructions.voidsetDocument(File file)Renders a Document read from an InputStream using a URL as a base URL for relative paths.voidsetDocument(InputStream stream, String url)Renders a Document read from an InputStream using a URL as a base URL for relative paths.voidsetDocument(String uri)Loads and renders a Document given an uri.voidsetDocument(Document doc)Renders an XML Document instance.voidsetDocument(Document doc, String url)Renders a Document using a URL as a base URL for relative paths.voidsetFontScalingFactor(float scaling)Sets the scaling factor used byincrementFontSize()anddecrementFontSize()--both scale the font up or down by this scaling factor.voidsetMaxFontScale(float f)Sets the maximum font scaling that may be applied, e.g.voidsetMinFontScale(float f)Sets the minimum font scaling that may be applied, e.g.voidsetSharedContext(SharedContext ctx)Sets theRenderingContextattribute of the XHTMLPanel object.-
Methods inherited from class org.xhtmlrenderer.swing.BasicPanel
addMouseTrackingListener, assignPagePrintPositions, doRender, getDocument, getDocumentTitle, getMouseTrackingListeners, getURL, isCenteredPagedView, isInteractive, isOpaque, loadDocument, paintComponent, paintPage, printTree, reloadDocument, reloadDocument, removeMouseTrackingListener, resetMouseTracker, scrollTo, setCenteredPagedView, setDocument, setDocument, setDocumentFromString, setDocumentRelative, setFormSubmissionListener, setInteractive, setLayout, setOpaque, setSize, submit
-
Methods inherited from class org.xhtmlrenderer.swing.RootPanel
addDocumentListener, addNotify, doDocumentLayout, doLayout, find, find, fireDocumentLoaded, fireDocumentStarted, fireOnLayoutException, fireOnRenderException, getCellRendererPane, getEnclosingScrollPane, getFixedRectangle, getLayoutContext, getLayoutWidth, getPreferredScrollableViewportSize, getRootBox, getRootLayer, getScreenExtents, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSharedContext, hasDocumentListeners, init, isActive, isDefaultFontFromComponent, isExtentsHaveChanged, isFocus, isHover, isNeedRelayout, isPrintView, newLayoutContext, newRenderingContext, removeDocumentListener, removeNotify, repaintRequested, resetScrollPosition, setDefaultFontFromComponent, setDocument, setEnclosingScrollPane, setNeedRelayout, setRootBox, validate
-
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
-
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validateTree
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Constructor Detail
-
XHTMLPanel
public XHTMLPanel()
Instantiates an XHTMLPanel with noDocumentloaded by default.
-
XHTMLPanel
public XHTMLPanel(UserAgentCallback uac)
Instantiates a panel with a customUserAgentCallbackimplementation.- Parameters:
uac- The custom UserAgentCallback implementation.
-
-
Method Detail
-
relayout
public void relayout()
Lays out the current document again, and re-renders.
-
setDocument
public void setDocument(String uri)
Loads and renders a Document given an uri. The uri is resolved by the UserAgentCallback- Overrides:
setDocumentin classBasicPanel
-
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:
setDocumentin classBasicPanel- Parameters:
doc- The new document valueurl- 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:
setDocumentin classBasicPanel- 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 theRenderingContextattribute 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 usingRootPanel.getSharedContext()to retrieve the current context, and using mutators to change its behavior.- Overrides:
setSharedContextin classBasicPanel- Parameters:
ctx- A new RenderingContext to use for rendering.
-
setFontScalingFactor
public void setFontScalingFactor(float scaling)
Sets the scaling factor used byincrementFontSize()anddecrementFontSize()--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 bysetFontScalingFactor(float), and reset to the document's specified font size withresetFontSize().
-
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 bysetFontScalingFactor(float), and reset to the document's specified font size withresetFontSize().
-
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.
-
-