Class HtmlPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class HtmlPane extends JComponent
A widget for displaying HTML documents. This class wraps a JEditorPane in a JScrollPane and provides a set of buttons for navigation. Links that appear in the HTML may be clicked to visit a URL. The buttons provide the following functions.
  • The "start" button takes you to the URL provided by the setPage method. It is equivalent to the maximum possible number of repetitions of the "back" button.
  • The "back" button returns to the page that was last visited by clicking on a link.
  • The "forw" button moves forward, undoing the effects of pushing the "back" button.
  • The "end" button is equivalent to the repeated use of the "forw" button.
  • The "reload" button reloads the link currently visited.
There is keyboard support for scrolling within a window, with additional shortcuts using some emacs conventions:
  • the DOWN-ARROW key, plus C-N , moves the view down by roughly one line.
  • the UP-ARROW key, plus C-P, moves the view up by roughly one line.
  • the PAGE-DOWN key moves the view down by one page, where a page is the size of the window.
  • the PAGE-UP key moves the view up by one page, where a page is the size of the window.
  • the HOME key, plus M-<, moves the view to the start of the document.
  • the END key, plus M-<, moves the view to the end of the document.

Note: The buttons contain icons instead of text, but have text-based tooltips. The icons are in resources named org/bzdev/swing/icons/fleft.gif, org/bzdev/swing/icons/left.gif, org/bzdev/swing/icons/redo.gif, org/bzdev/swing/icons/right.gif, org/bzdev/swing/icons/fright.gif, and org/bzdev/swing/icons/rlredo.gif. These are supplemented for 'reverse video' by icons in the filesorg/bzdev/swing/icons/fleftRV.gif, org/bzdev/swing/icons/leftRV.gif, org/bzdev/swing/icons/redoRV.gif, org/bzdev/swing/icons/rightRV.gif,org/bzdev/swing/icons/frightRV.gif, and org/bzdev/swing/icons/rlredoRV.gif for enabled icons. The disabled icons for reverse video are in resources named org/bzdev/swing/icons/fleftRVD.gif org/bzdev/swing/icons/leftRVD.gif, org/bzdev/swing/icons/redoRVD.gif, org/bzdev/swing/icons/rightRVD.gif, org/bzdev/swing/icons/frightRVD.gif, and org/bzdev/swing/icons/rlredoRVD.gif.

The MIME types supported are those that a JEditorPane supports.

Internationalization is handled by the resource org/bzdev/swing/HtmlPaneBundle.properties. This defines the strings startText, backText, reloadText, frwdText, endText. as values to use in buttons if icons are not provided. The strings startTip, backTip, reloadTip, frwdTip, and endTip provide corresponding tool tips. Finally, the string direction indicates if the components should be laid out LR (left to right) or RL (right to left.) This also has an effect on the how icons are assigned to buttons. The icon rlredo.gif is the mirror image of redo.gif and is provided for right-to-left layouts.

One can set the HTML page displayed by calling one of the setPage methods or by using a constructor that takes a URL or a String specifying a URL as an argument.

This component allows HTML pages to be displayed but does not allow them to be edited.

A few methods allow one to set colors to implement a "dark mode" for viewing HTML pages in reverse video. This does change an HTML page's appearance: to change it (e.g., to make the text white and the background dark), a style sheet can be used.

The behavior specified for JEditorPane in Java 11 is such that the constuctor HtmlPane() configure a JEditorPane so that JEditorPane.getPage() will return null even if setPage(URL) or setPage(String) was called. This may affect address resolution. The method getPage() always returns the page that was last set.

See Also:
  • Field Details

    • errorTitle

      protected String errorTitle
      The string that will be displayed as a frame title when a dialog box reporting an error is displayed. The default is obtained from the resource bundle org.bzdev.swing.lpack.HtmlPaneBundle
  • Constructor Details

    • HtmlPane

      public HtmlPane()
      Constructor.
    • HtmlPane

      public HtmlPane(String url) throws IOException
      Constructor based on a url represented by a string.
      Parameters:
      url - the URL that the pane should initially display
      Throws:
      IOException - an error setting the page occured.
    • HtmlPane

      public HtmlPane(URL url) throws IOException
      Constructor based on a url.
      Parameters:
      url - the URL that the pane should initially display
      Throws:
      IOException - an error setting the page occurred.
  • Method Details

    • setLocale

      public void setLocale(Locale locale)
      Overrides:
      setLocale in class Component
    • setContentLocale

      public void setContentLocale(Locale locale)
      Set the local of the content pane. This method allows the locale of the content panes (the HTML pane) to be set without changing the layout of navigation controls.
      Parameters:
      locale - the locale.
    • getContentPaneBorder

      public Border getContentPaneBorder()
      Get the border for this component's contents.
      Returns:
      the border
    • setContentPaneBorder

      public void setContentPaneBorder(Border border)
      Set the border for this component's contents.
      Parameters:
      border - the border
    • setPage

      public void setPage(String page) throws IOException
      Set the page to display and display it.
      Parameters:
      page - the URL of the page to display.
      Throws:
      IOException - an error occurred reading the page.
    • setPage

      public void setPage(URL page) throws IOException
      Set the page to display and display it.
      Parameters:
      page - the URL of the page to display.
      Throws:
      IOException - an error occurred reading the page.
    • setBackground

      public void setBackground(Color color)
      Set the background color. This method will also set the background color of scroll bars unless the last call to setScrollbarBackground(Color), if one was made, had a non-null argument.

      Note: with some pluggable look and feels, this method may be fully or partially ignored. It works as expected with the default look and feel.

      Overrides:
      setBackground in class JComponent
      Parameters:
      color - the color
    • setScrollbarBackground

      public void setScrollbarBackground(Color color)
      Set the background color for scroll bars. If color is null and the last call to setBackground(Color), if one was made, had a non-null argument, that background color will be used. If the last call to setBackground(Color) had a null argument, the color passed as an argument will be used.

      Note: with some pluggable look and feels, this method may be fully or partially ignored. It works as expected with the default look and feel.

      Parameters:
      color - the color; null for the default color as modified by the last call (if any) to setBackground(Color)
    • getPage

      public URL getPage()
      get the URL for the page being displayed.
      Returns:
      the URL for the page being displayed; null if there is none.
    • setErrorTitle

      public final void setErrorTitle(String title)
      Set the title to use in error message dialog boxes. This title will be used as a title for various dialog boxes.
      Parameters:
      title - the title to display; null to use the default (which is locale specific)
    • getErrorTitle

      public final String getErrorTitle()
      Get the title used in error message dialog boxes.
      Returns:
      the current title.
    • setComponentOrientation

      public void setComponentOrientation(ComponentOrientation o)
      Overrides:
      setComponentOrientation in class Component
    • setButtonBackground

      public void setButtonBackground(Color color, boolean rvmode)
      Set the background color for the 'start', 'back', 'reload', 'forward', and 'end' controls. When rvmode is true, the icons will be white when enabled and grey when disabled, the reverse from the normal behavior.

      Note: with some pluggable look and feels, this method may be fully or partially ignored. It works as expected with the default look and feel.

      Parameters:
      color - the color
      rvmode - true for reverse video; false otherwise
    • isEditable

      public boolean isEditable()
      Determine if this HtmlPane is editable.
      Returns:
      true if it is editable; false otherwise
    • setEditable

      public void setEditable(boolean value)
      Set whether or not this HtmlPane is editable. This method sets a property of the underlying JEditorPane.

      When editable, one can add text, but to format it a subclass will be needed. When editing, links will not function and the text will not be modifiable.

      Parameters:
      value - true if this pane is to be editable; false otherwise
    • setCaretColor

      public void setCaretColor(Color c)
      Set the color of the caret. The caret (by default a blinking vertical line) will normally be black. It should be set to white if the background will be dark.
      Parameters:
      c - the color for the caret