Adding Functionality to the Basic Text Components

Java > Core SWING advanced programming > 4. JEDITORPANE AND THE SWING HTML PACKAGE

 

Chapter 4. JEDITORPANE AND THE SWING HTML PACKAGE

Topics in this Chapter

  • Displaying and Saving HTML, RTF, and Plain Text

  • Asynchronous Page Loading

  • Converting Between Different Document Formats

  • The HTML Document Model

  • Loading and Parsing HTML

  • Handling Hypertext Links

  • Style Sheets and Views

  • Dynamically Changing HTML in a JEditorPane

In Chapter 2, you saw how to use the JTextPane control to display text with multiple fonts and colors and how to embed images and other active content into it. To use JTextPane in this way, you need to do quite a lot of programming, manually setting up both the text and the corresponding attributes. Although this might be acceptable in some applications, perhaps because the text to be displayed is fixed at compile time or the algorithm used to apply the attributes is a simple one that is based on the text itself, for more complex text layouts it quickly becomes unwieldy. What you actually need is a way to store the text and the attributes together so that the attributes can be applied as the text is read into the component. From the development and maintenance point of view, it would be even simpler if the storage format were easily readable and editable by a human or could be created and manipulated by a tool that can be used without the need for much (or any) programming ability.

There are, of course, several file formats that meet this requirement. Hypertext Markup Language (HTML), for example, is a very well known and popular way to represent complex document layouts that encapsulates the text (and other content) along with a representation of the attributes used to render it. Although many people will create HTML pages manually (and this could be considered to be a form of programming), these days there are plenty of HTML editors that let you create a Web page using HTML without needing to understand the format of the Web page itself. These editors ultimately write their output to a text file that can be loaded into a browser. This sounds like an ideal way to provide input to a JTextPane. All you need is some software that reads HTML and converts it into the sort of Document model that you saw in Chapter 2. In fact, Swing has a component that can do just that. JEditorPane is a superclass of JTextPane that provides the ability to plug in software that knows how to read external document formats and convert what it reads into the appropriate Document model for JEditorPane to display. Furthermore, Swing provides plug-in document readers that allow JEditorPane to read and display documents encoded in HTML and Rich Text Format (RTF) as well as in plain text. In this chapter, we'll first look at the general features of JEditorPane and show how to use it to display different types of document. We'll then look at how the various classes that a JEditorPane is made up of combine to make it possible for it to handle different input formats and, finally, we'll take an in-depth look at Swing's HTML support and show you how to customize it and make best use of its features.

 

 



Core Swing
Core Swing: Advanced Programming
ISBN: 0130832928
EAN: 2147483647
Year: 1999
Pages: 55
Authors: Kim Topley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net