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
.
Related Topics
How to Pass an Object from One Activity to Another on Android
How to Read a Text File in Android
Android: Internet Connectivity Change Listener
Limit Jvm Process Memory on Ubuntu
How to Create a Generic Array in Java
Convert a String Representation of a Hex Dump to a Byte Array Using Java
What Is the Java : Operator Called and What Does It Do
"Comparison Method Violates Its General Contract!"
Why Can't I Use Switch Statement on a String
Run Java Class File from PHP Script on a Website
Nullpointerexception Accessing Views in Oncreate()
Java.Text.Parseexception: Unparseable Date: Java.Text.Dateformat.Parse(Dateformat.Java:579)
Parsing Query Strings on Android
Why Maven Uses Jdk 1.6 But My Java -Version Is 1.7
Int Division: Why Is the Result of 1/3 == 0
Cannot Refer to a Non-Final Variable Inside an Inner Class Defined in a Different Method