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) {
}
});
}
}
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 classTag
, it also contains a large number of finalTag
instances that represent each of the HTML tags.We're interested in the line;
javax.swing.text.html.HTMLDocument
contains aHashTable
tagMap
that stores all of these tags.We're interested in the lines;
Where
ca
isTagAction ca = new CharacterAction();
and;I've not located the package for
TagAction
yet or a way of accessing/changing the HTML document used byHTMLEditorKit
.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
So it looks asthough we need to change whatever builds the
attributeSet
s within the document being written.