Class XHTMLPanel
- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
,Scrollable
,FSCanvas
,UserInterface
,FormSubmissionListener
,RepaintListener
- Direct Known Subclasses:
ScalableXHTMLPanel
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.*;
// 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:
<pre>
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.cool.dom.DomImpl");
</pre>
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:
-
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
FieldsModifier and TypeFieldDescriptionprivate float
private float
private float
Fields inherited from class org.xhtmlrenderer.swing.BasicPanel
formSubmissionListener
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
ConstructorsConstructorDescriptionInstantiates an XHTMLPanel with noDocument
loaded by default.Instantiates a panel with a customUserAgentCallback
implementation. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Decrements all rendered fonts on the current document by the current scaling factor for the panel.float
Returns the maximum font scaling that may be applied, e.g.float
Returns the minimum font scaling that may be applied, e.g.void
Increments all rendered fonts on the current document by the current scaling factor for the panel.void
Resets all rendered fonts on the current document to the font size specified in the document's styling instructions.private void
private void
scaleFont
(float scaleBy) Applies a change in scale for fonts using the rendering context's text renderer.void
setDocument
(File file) Renders a Document read from an InputStream using a URL as a base URL for relative paths.void
setDocument
(InputStream stream, String url) Renders a Document read from an InputStream using a URL as a base URL for relative paths.void
setDocument
(String uri) Loads and renders a Document given an uri.void
setDocument
(Document doc) Renders an XML Document instance.void
setDocument
(Document doc, @Nullable String url) Renders a Document using a URL as a base URL for relative paths.void
setFontScalingFactor
(float scaling) Sets the scaling factor used byincrementFontSize()
anddecrementFontSize()
--both scale the font up or down by this scaling factor.void
setMaxFontScale
(float f) Sets the maximum font scaling that may be applied, e.g.void
setMinFontScale
(float f) Sets the minimum font scaling that may be applied, e.g.private void
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, relayout, 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
-
Field Details
-
fontScalingFactor
private float fontScalingFactor -
minFontScale
private float minFontScale -
maxFontScale
private float maxFontScale
-
-
Constructor Details
-
XHTMLPanel
public XHTMLPanel()Instantiates an XHTMLPanel with noDocument
loaded by default. -
XHTMLPanel
Instantiates a panel with a customUserAgentCallback
implementation.- Parameters:
uac
- The custom UserAgentCallback implementation.
-
-
Method Details
-
setupListeners
private void setupListeners() -
resetListeners
private void resetListeners() -
setDocument
Loads and renders a Document given an uri. The uri is resolved by the UserAgentCallback- Overrides:
setDocument
in classBasicPanel
-
setDocument
Renders an XML Document instance. Make sure that no relative resources are needed- Parameters:
doc
- The document to render.
-
setDocument
Renders a Document using a URL as a base URL for relative paths.- Overrides:
setDocument
in classBasicPanel
- Parameters:
doc
- The new document valueurl
- The new document value
-
setDocument
Renders a Document read from an InputStream using a URL as a base URL for relative paths.- Overrides:
setDocument
in classBasicPanel
- Parameters:
stream
- The stream to read the Document from.url
- The URL used to resolve relative path references.
-
setDocument
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
-
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()
. -
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.
-