Using a JEditorPane to display a web page. import *; import *; import *; import *; public class. The JEditorPane class is the foundation for Swing’s styled text components and provides a mechanism Using an Editor Pane to Display Text From a URL. This class implements accessibility support for the JEditorPane class. Gets the current URL being displayed. getRef method for the URL being displayed).

Author: Zulkijinn Malasar
Country: Martinique
Language: English (Spanish)
Genre: Literature
Published (Last): 6 May 2015
Pages: 454
PDF File Size: 18.26 Mb
ePub File Size: 19.39 Mb
ISBN: 192-3-71101-873-6
Downloads: 36560
Price: Free* [*Free Regsitration Required]
Uploader: Dogal

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.

showing images on jeditorpane (java swing) – Stack Overflow

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: The kit used in this case is an extension of DefaultEditorKit that produces a wrapped plain text view. The kit used in this case is the class javax. There are several ways to load content into this component. 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. The read method can be used to initialize the component from a Reader. Note that if the content type is HTML, relative references e. 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. Some kinds of content may provide hyperlink support by generating hyperlink events. 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. 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. There are multiple ways to get a character set mapping to happen with JEditorPane. 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. 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.

Display html string in JEditorPane in Java

Newlines For a discussion on how newlines are handled, see DefaultEditorKit. Swing is not thread safe. For more information see Swing’s Threading Jeditorpaane. 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.

String getContentType Gets the type of content that this editor is currently set to deal with. EditorKit getEditorKit Fetches the currently installed kit for handling content. String getText Returns the text contained in this TextComponent jeditirpane terms of the content type of this editor.


By default this is not enabled; to enable it set the client property with this name to Boolean. The default varies based on the look and feel; to enable it set the client property with this name to Boolean. The document model is jeditoorpane to null. This is a convenience constructor that calls the setContentType and setText methods.

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.

The content type of the pane is set, and if the editor kit for the pane is non- nullthen 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. Jeditorpan following code shows how this can be done: 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.

JEditorPane (Java Platform SE 8 )

This may load either synchronously or asynchronously depending upon the document returned by the EditorKit. 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 setDocumentwhich 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 displag for it by the UI which will then be scrolled if necessary, and then ejditorpane 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 dispkay 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 jedihorpane 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. IOException – for a null or invalid page specification, or exception from the stream being read See Also: Otherwise it calls the superclass method which loads the model as plain text. IOException – as thrown by the stream being used to initialize See Also: 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 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.

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.

If a URL was not specified in the creation of the document, this will return nulland relative URL’s will not be resolved. This is defined to be the type associated with the currently installed EditorKit. This is mostly convenience method that can be used as an alternative to calling setEditorKit directly.


If there is a charset definition specified as a parameter of the content type specification, it will be used when loading input streams using the associated EditorKit. NullPointerException – if the type parameter is null See Also: This is the bound property that establishes the content type of the editor. Any old kit is first deinstalled, then if kit is non- nullthe new kit is installed, and a default document created for it. This has the side effect of changing the model, because the EditorKit is the source of how a particular type of content is modeled.

This method will cause setDocument to be called on behalf of the caller to ensure integrity of the internal state. This is called when a type is requested that doesn’t match the currently installed type. If the component doesn’t have an EditorKit registered for the given type, it will try to create an EditorKit from the default EditorKit registry. If that fails, a PlainEditorKit is used on the assumption that all text documents can be represented as plain text.

This method can be reimplemented to use some other kind of type registry. This can be reimplemented to use the Java Activation Framework, for example. A look-and-feel implementation might use this in conjunction with createEditorKitForContentType to install handlers for content types with a look-and-feel bias. If there is no selection this amounts to an insert of the given text.

If there is no replacement text i. The replacement text will have the attributes currently defined for input. If the component is not editable, beep and return. The registry is created if necessary. If the registered class has not yet been loaded, an attempt is made to dynamically load the prototype of the kit for the given type.

If the type was registered with a ClassLoaderthat ClassLoader will be used to load the prototype. If there was no registered ClassLoaderClass. Once a prototype EditorKit instance is successfully located, it is cloned and the clone is returned.

The class will be dynamically loaded later when actually needed, and can dipslay safely changed before attempted uses to avoid loading unwanted classes. The prototype EditorKit will be loaded with Class. The class will be dynamically loaded later when actually needed using the given ClassLoaderand can be safely changed before attempted uses to avoid loading jedirorpane classes. The preferred size for JEditorPane is slightly altered from the preferred size of the superclass.

If the size of the viewport has become smaller than the minimum size of the component, the scrollable definition for tracking width or height will turn to false. The default viewport layout will give the preferred size, and that is not desired in the case where the scrollable is tracking.

Using a JEditorPane to display a web page

In that case the normal preferred size is adjusted to the minimum size. This allows things like HTML tables to shrink down to their minimum size and then be laid out at their minimum size, refusing to shrink any further. This is implemented to remove the contents of the current document, and replace them by parsing the given string using the current EditorKit. This gives the semantics of the superclass by not changing out the model, while supporting the content type currently set on this component.