Inconsistent Performance Applying Foregroundactions in a Jeditorpane When Reading Html

Java Swing JEditorPane insert bold tags around selection

i'm not sure, but you can try this code.

 // toggle bold tags around the text
String txt = doc.getSelectedText();
if (selectStart != selectEnd) {
try {
// doc.insertAfterStart(startElem, "<b>WTF</b>");
doc.remove(selectStart, selectEnd-selectStart);
ekit.insertHTML((HTMLDocument) doc.getDocument(), selectStart, "<b>"+txt+"</b>", 0, 0, HTML.Tag.B);
} catch (BadLocationException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}

Text formatting in JEditorPane

Cast it to styled document

((StyledDocument)editorPane.getDocument()).setCharacterAttributes(start,length,myTextStyle,false);

How to use TextAction

While composing this answer, I recalled a venerable HTMLDocumentEditor by Charles Bell that illustrates the typical usage of the subclasses found in javax.swing.text.TextAction. That editor is listed among the credits of Metaphase Editor. This related example showing actions found in StyledEditorKit follows the same approach. All such actions are suitable for Key Bindings, and all operate on the current selection maintained by the Caret, whenever possible.

Java - Set Font size of selection in a JEditorPane?

There's a working example here that uses StyledEditorKit.FontSizeAction and related classes in StyledEditorKit.

image



Related Topics



Leave a reply



Submit