Enforcing <strong> instead of <b> in HTMLEditorKit/JEditorPane

238 Views Asked by At

I am trying to ensure that the JEditorPane with HTMLEditorKit uses <strong> tags instead of <b> tags. The code below loads a JFrame with an JEditorPane. Try selecting a part of the text and clicking the button to turn the selection bold. The System.out shows that the bolding is being caused by tags.

How do I set it up so that it is XHTML compliant and uses tags instead?

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLEditorKit;

public class BStrongTest extends JPanel {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new BStrongTest());
        frame.setSize(300, 200);

        frame.setVisible(true);
    }

    public BStrongTest() {
        setLayout(new BorderLayout());

        final JEditorPane pane = new JEditorPane();
        pane.setEditorKit(new HTMLEditorKit());
        pane.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent bibendum.");
        add(pane, BorderLayout.NORTH);

        JButton boldButton = new JButton();
        boldButton.setAction(new StyledEditorKit.BoldAction());
        boldButton.setText("Boldify");
        add(boldButton, BorderLayout.SOUTH);

        pane.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                System.out.println(pane.getText());
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
            }
        });
    }
}
1

There are 1 best solutions below

4
On

This answer is incomplete, I'm using it as a notepad when I keep coming back to continue.

javax.swing.text.html.HTML contains a class Tag, it also contains a large number of final Tag instances that represent each of the HTML tags.

We're interested in the line;

public static final Tag STRONG = new Tag("b");

javax.swing.text.html.HTMLDocument contains a HashTable tagMap that stores all of these tags.

We're interested in the lines;

tagMap.put(HTML.Tag.STRONG, ca);

Where ca is TagAction ca = new CharacterAction(); and;

protected void registerTag(HTML.Tag t, TagAction a) {
    tagMap.put(t, a);
}

I've not located the package for TagAction yet or a way of accessing/changing the HTML document used by HTMLEditorKit.


I've located what I believe to be the point where a tag is written out (marked with the //here;

javax.swing.java.text.html.HTMLWriter.java

protected void writeEmbeddedTags(AttributeSet attr) throws IOException {

    // translate css attributes to html
    attr = convertToHTML(attr, oConvAttr);

    Enumeration names = attr.getAttributeNames();
    while (names.hasMoreElements()) {
        Object name = names.nextElement();
        if (name instanceof HTML.Tag) {
            HTML.Tag tag = (HTML.Tag)name;
            if (tag == HTML.Tag.FORM || tags.contains(tag)) {
                continue;
            }
            write('<');
            write(tag.toString());//Here
            Object o = attr.getAttribute(tag);
            if (o != null && o instanceof AttributeSet) {
                writeAttributes((AttributeSet)o);
            }
            write('>');
            tags.addElement(tag);
            tagValues.addElement(o);
        }
    }
}

So it looks asthough we need to change whatever builds the attributeSets within the document being written.