Insert Bulleted Or Numbered List Using HTML In JTextPane
Posted by tech on
November 30, 2010
JTextPane does not have bulleted list or numbered list support. While making it happen using either StyledEditorKit or RTFEditorKit is not possible unless you create your own, I figure the easiest way to implement this feature would be through the use of an HTMLEditorKit since HTML supports both unordered and ordered lists.
The code below inserts HTML code of an unordered list into the JTextPane. For ordered or numbered lists, you can change the variables of the code and replace the <UL> HTML tag to <OL>.
Action ulAction = new HTMLEditorAction("Unordered list", bulletButton.getIcon()) { @Override public void actionPerformed(ActionEvent e) { ulActionB_actionPerformed(e); } }; JButton bulletButton = new JButton(); bulletButton.setAction(ulAction); bulletButton.setText("bulleted list"); public void ulActionB_actionPerformed(ActionEvent e) { String parentname = ((HTMLDocument) editor.getDocument()) .getParagraphElement(editor.getCaretPosition()) .getParentElement() .getName(); HTML.Tag parentTag = HTML.getTag(parentname); HTMLEditorKit.InsertHTMLTextAction ulAction = new HTMLEditorKit.InsertHTMLTextAction( "insertUL", "", parentTag, HTML.Tag.UL); ulAction.actionPerformed(e); }








