Search code examples
javahtmlswingsyntax-highlightingjtextpane

JTextPane Syntax-Highlight - Restricting my formatting?


I'm currently programming an IDE, and am using a JTextPane as the "code-editor".

Now, I am trying to add syntax highlighting to the JTextPane, but am having some trouble...

I'm using HTML to replace http://img21.imageshack.us/img21/6185/910b3b10cad4487f9c96d43.png with Green Comment :

private void jTextPane1KeyTyped(java.awt.event.KeyEvent evt) {                                    

        String SyntaxedCode = jTextPane1.getText();

        jTextPane1.setText(SyntaxedCode.replaceAll("//", "<span style='color: green'>//</span>"));

}                                   

Getting The Result:

enter image description here

Now, the part I'm struggling with, is the fact that the JTextPane is limiting my spacing... I can only add one or two spaces in a row, and then the JTextPane just stops accepting spaces. Also, I can't use the enter key to go to a new line.

Why is it restricting me like this, and how can I avoid it?

Thanks in advance;


Solution

  • 1) for listening events from keyboard to the JTextComponents, then you have implements DocumentListener

    2) in the case of you want to filtering, block or modify inputs from keyboard, and before this contents will be diplayed in the GUI, then you have look for DocumentFilter

    3) I'd suggest to implements Highlighter, sure is possible to use Html formatted text, then better is to create own HtlmEditorKit

    4) forgot for listeng in the Swing JComponents by using KeyListener, use KeyBindings