O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The HTMLEditorKit Class

The API for the HTMLEditorKit is more or less what you might expect from an editor kit if you made it through Chapter 19-Chapter 22 on Swing text components. We’ll take a brief look at the API and then go through examples of how to work with this editor kit.

Inner Classes

As you can see in Figure 23-7, HTMLEditorKit defines several inner classes. These inner classes are integral to the display and editing of HTML content. While we don’t want to spend too much time on the details of these classes, you should know about them. We list them here as a quick reference.

public static class HTMLEditorKit.HTMLFactory

The view factory implementation for HTML.

public static class HTMLEditorKit.HTMLTextAction

An Action for inserting HTML into an existing document.

public static class HTMLEditorKit.InsertHTMLTextAction

An extension of HTMLTextAction that allows you to insert arbitrary HTML content. This one is quite handy. For example, we can create an action for a toolbar or menu that inserts a copyright:

private final static String COPY_HTML =
  "<p>&copy; 2002, O'Reilly &amp; Associates</p>";
Action a = new HTMLEditorKit.InsertHTMLTextAction("InsertCopyright", 
    COPY_HTML, HTML.Tag.BODY, HTML.Tag.P);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/cpyrght.gif"));
a.putValue(Action.NAME, "Copyright Text");

The action can be added to a toolbar or menu, just like the others in the StyledEditor example. We end up with a one-touch button that adds our copyright information to the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required