Java html in jeditorpane

Class JEditorPane

This component uses implementations of the EditorKit to accomplish its behavior. It effectively morphs into the proper kind of text editor for the kind of content it is given. The content type that editor is bound to at any given time is determined by the EditorKit currently installed. If the content is set to a new URL, its type is used to determine the EditorKit that should be used to load the content.

By default, the following types of content are known: text/plain Plain text, which is the default the type given isn’t recognized. The kit used in this case is an extension of DefaultEditorKit that produces a wrapped plain text view. text/html HTML text. The kit used in this case is the class javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support. text/rtf RTF text. The kit used in this case is the class javax.swing.text.rtf.RTFEditorKit which provides a limited support of the Rich Text Format.

  1. The setText method can be used to initialize the component from a string. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
  2. The read method can be used to initialize the component from a Reader . Note that if the content type is HTML, relative references (e.g. for things like images) can’t be resolved unless the tag is used or the Base property on HTMLDocument is set. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
  3. The setPage method can be used to initialize the component from a URL. In this case, the content type will be determined from the URL, and the registered EditorKit for that content type will be set.
Читайте также:  Developer Helps | Display None

Some kinds of content may provide hyperlink support by generating hyperlink events. The HTML EditorKit will generate hyperlink events if the JEditorPane is not editable ( JEditorPane.setEditable(false); has been called). If HTML frames are embedded in the document, the typical response would be to change a portion of the current document. The following code fragment is a possible hyperlink listener implementation, that treats HTML frame events specially, and simply displays any other activated hyperlinks.

class Hyperactive implements HyperlinkListener < public void hyperlinkUpdate(HyperlinkEvent e) < if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) < JEditorPane pane = (JEditorPane) e.getSource(); if (e instanceof HTMLFrameHyperlinkEvent) < HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e; HTMLDocument doc = (HTMLDocument)pane.getDocument(); doc.processHTMLFrameHyperlinkEvent(evt); >else < try < pane.setPage(e.getURL()); >catch (Throwable t) < t.printStackTrace(); >> > > >

For information on customizing how text/html is rendered please see W3C_LENGTH_UNITS and HONOR_DISPLAY_PROPERTIES

Culturally dependent information in some documents is handled through a mechanism called character encoding. Character encoding is an unambiguous mapping of the members of a character set (letters, ideographs, digits, symbols, or control functions) to specific numeric code values. It represents the way the file is stored. Example character encodings are ISO-8859-1, ISO-8859-5, Shift-jis, Euc-jp, and UTF-8. When the file is passed to an user agent ( JEditorPane ) it is converted to the document character set (ISO-10646 aka Unicode).

  1. One way is to specify the character set as a parameter of the MIME type. This will be established by a call to the setContentType method. If the content is loaded by the setPage method the content type will have been set according to the specification of the URL. It the file is loaded directly, the content type would be expected to have been set prior to loading.
  2. Another way the character set can be specified is in the document itself. This requires reading the document prior to determining the character set that is desired. To handle this, it is expected that the EditorKit .read operation throw a ChangedCharSetException which will be caught. The read is then restarted with a new Reader that uses the character set specified in the ChangedCharSetException (which is an IOException ).
Читайте также:  Example of HTML caption Tag

Warning: Swing is not thread safe. For more information see Swing’s Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder .

Nested Class Summary

This class provides support for AccessibleHypertext , and is used in instances where the EditorKit installed in this JEditorPane is an instance of HTMLEditorKit .

Nested classes/interfaces declared in class javax.swing.text.JTextComponent

Nested classes/interfaces declared in class javax.swing.JComponent

Nested classes/interfaces declared in class java.awt.Container

Nested classes/interfaces declared in class java.awt.Component

Field Summary

Key for a client property used to indicate whether the default font and foreground color from the component are used if a font or foreground color is not specified in the styled text.

Key for a client property used to indicate whether w3c compliant length units are used for html rendering.

Fields declared in class javax.swing.text.JTextComponent

Fields declared in class javax.swing.JComponent

Fields declared in class java.awt.Component

Fields declared in interface java.awt.image.ImageObserver

Constructor Summary

Method Summary

Adds a hyperlink listener for notification of any changes, for example when a link is selected and entered.

Returns an array of all the HyperLinkListener s added to this JEditorPane with addHyperlinkListener().

Returns true if a viewport should always force the height of this Scrollable to match the height of the viewport.

Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.

Scrolls the view to the given reference location (that is, the value returned by the URL.getRef method for the URL being displayed).

Sets the text of this TextComponent to the specified content, which is expected to be in the format of the content type of this editor.

Methods declared in class javax.swing.text.JTextComponent

Methods declared in class javax.swing.JComponent

Methods declared in class java.awt.Container

Methods declared in class java.awt.Component

Methods declared in class java.lang.Object

Field Details

W3C_LENGTH_UNITS

Key for a client property used to indicate whether w3c compliant length units are used for html rendering. By default this is not enabled; to enable it set the client property with this name to Boolean.TRUE .

HONOR_DISPLAY_PROPERTIES

Key for a client property used to indicate whether the default font and foreground color from the component are used if a font or foreground color is not specified in the styled text. The default varies based on the look and feel; to enable it set the client property with this name to Boolean.TRUE .

Constructor Details

JEditorPane

JEditorPane

JEditorPane

JEditorPane

Creates a JEditorPane that has been initialized to the given text. This is a convenience constructor that calls the setContentType and setText methods.

Method Details

addHyperlinkListener

Adds a hyperlink listener for notification of any changes, for example when a link is selected and entered.

removeHyperlinkListener

getHyperlinkListeners

Returns an array of all the HyperLinkListener s added to this JEditorPane with addHyperlinkListener().

fireHyperlinkUpdate

Notifies all listeners that have registered interest for notification on this event type. This is normally called by the currently installed EditorKit if a content type that supports hyperlinks is currently active and there was activity with a link. The listener list is processed last to first.

setPage

@BeanProperty(expert=true, description=»the URL used to set content») public void setPage (URL page) throws IOException

Sets the current URL being displayed. The content type of the pane is set, and if the editor kit for the pane is non- null , then a new default document is created and the URL is read into it. If the URL contains and reference location, the location will be scrolled to by calling the scrollToReference method. If the desired URL is the one currently being displayed, the document will not be reloaded. To force a document reload it is necessary to clear the stream description property of the document. The following code shows how this can be done:

Document doc = jEditorPane.getDocument(); doc.putProperty(Document.StreamDescriptionProperty, null);

If the desired URL is not the one currently being displayed, the getStream method is called to give subclasses control over the stream provided. This may load either synchronously or asynchronously depending upon the document returned by the EditorKit . If the Document is of type AbstractDocument and has a value returned by AbstractDocument.getAsynchronousLoadPriority that is greater than or equal to zero, the page will be loaded on a separate thread using that priority. If the document is loaded synchronously, it will be filled in with the stream prior to being installed into the editor with a call to setDocument , which is bound and will fire a property change event. If an IOException is thrown the partially loaded document will be discarded and neither the document or page property change events will be fired. If the document is successfully loaded and installed, a view will be built for it by the UI which will then be scrolled if necessary, and then the page property change event will be fired. If the document is loaded asynchronously, the document will be installed into the editor immediately using a call to setDocument which will fire a document property change event, then a thread will be created which will begin doing the actual loading. In this case, the page property change event will not be fired by the call to this method directly, but rather will be fired when the thread doing the loading has finished. It will also be fired on the event-dispatch thread. Since the calling thread can not throw an IOException in the event of failure on the other thread, the page property change event will be fired when the other thread is done whether the load was successful or not.

read

This method initializes from a stream. If the kit is set to be of type HTMLEditorKit , and the desc parameter is an HTMLDocument , then it invokes the HTMLEditorKit to initiate the read. Otherwise it calls the superclass method which loads the model as plain text.

getStream

Fetches a stream for the given URL, which is about to be loaded by the setPage method. By default, this simply opens the URL and returns the stream. This can be reimplemented to do useful things like fetch the stream from a cache, monitor the progress of the stream, etc. This method is expected to have the side effect of establishing the content type, and therefore setting the appropriate EditorKit to use for loading the stream. If this the stream was an http connection, redirects will be followed and the resulting URL will be set as the Document.StreamDescriptionProperty so that relative URL’s can be properly resolved.

scrollToReference

Scrolls the view to the given reference location (that is, the value returned by the URL.getRef method for the URL being displayed). By default, this method only knows how to locate a reference in an HTMLDocument. The implementation calls the scrollRectToVisible method to accomplish the actual scrolling. If scrolling to a reference location is needed for document types other than HTML, this method should be reimplemented. This method will have no effect if the component is not visible.

getPage

Gets the current URL being displayed. If a URL was not specified in the creation of the document, this will return null , and relative URL’s will not be resolved.

Источник

Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here 🙂

JEditorPane and Html Document

1. Introduction to JEditorPane

The JEditorPane extends from Java Swing’s JTextComponent. So it supports various document types. One can use it to display Plain document (txt), Rich Text Document (rtf) and html document. In this example, we will create a JEditorPane and load a html document into it.

2. About The Example

Below screenshot shows the JEditorPane example we create here:

Display Html in JEditorPane Example

The example is a simple JFrame, which houses a JEditorPane in it. The editor pane is displaying a html table above and the user can scroll the document using the scrollbars. These scrolls are useful when the document is bigger than the editor pane. Now, we will proceed with the example.

3. Html Document for the Example

In this example, we are going to load a html document in the Editor Pane. The html document is a table with heading text. We no need to go into much detail about this html content. Name this a Test.html and place it along with the java files (Found in Code Reference Section).

Источник

Оцените статью