Enabling element and attribute changes

Before we take a closer look on table manipulation in the following chapters, some techniques used in application SimplyHTML for doing element and attribute changes shall be discussed here. Some functions are available through common methods in classes of package javax.swing.text and javax.swing.text.html, others need to be enabled by own methods.

Adding elements

To add an element such as table, row or cell tags to a document, application SimplyHTML uses methods insertBeforeStart and insertAfterEnd of class HTMLDocument. These methods accept an HTML string to be inserted before or after an existing element of the document.

Removing elements

Almost any removal from an HTML document can be done with method remove from class AbstractDocument. Method remove is passed the start position inside the document and the length of the portion to remove. For some reason, this does not work on the last column of a table (explanations welcome!). An additional method removeElements in class SHTMLDocument is provided as an additional way to remove elements working for the last table column too. This method does basically the same as remove.

Changing attributes

Adding, removing and changing arbitrary attributes all can be done with the help of class MutableAttributeSet and its subclasses. A MutableAttributeSet is created by getting an AttributeSet from an Element and casting it to a MutableAttributeSet. Changes to that MutableAttributeSet then directly affect the Element the attributes belong to.

HTMLDocument however does not provide a way to change attributes in such way. Class SHTMLDocument therefore delivers a method addAttributes for changing attributes of an Element instead.