Implement text style on the fly in JEditorPane

I have an idea to write a note taking application in Java that does some simple text replacement as you type; probably a small subset of Markdown syntax. For example, if you entered a line that was #sometext, it would have turned out:

SOMETEXT

Wikidpad is part of what I'm trying to do (I'm not just re-implementing this, just part of the syntax / display functionality).

So, in essence, I’m looking for a way to write some component that, when the user hits “return”, he will look at the entered line and do any magic necessary for the html tags to display correctly.

I hit my head against Document Documents and DocumentFilters, as well as DocumentListeners and HTMLReaders, and without too much luck, can anyone put me on the right track?

+5
source share
5 answers

What you need is a set of java text editors . This tutorial was great and helped me implement a wiki editor that looks like your problem. The Swing Editor Kit (believe it or not) is specifically designed to solve your problem. I wrote a dynamic wiki as a reference system using this technique, although not without a great deal of struggle. Here is what I had to do:

  • , , . JavaDocument , insertUpdate. , , . , , . .

  • . , , . StyleContext. , . , - .

  • . .

, , , . , . .

, . , , , , . !

+3

, , - , , . , , .

JEditorPanes .. , , swing.

, , , , .

, .

+1

, , ?

, , , , JEditorPane.getText(), JEditorPane.setText();

, " ", . InputMethodEvent "getText()", . , Enter, , \n .

0

JTextPane, . KeyPressed:

jTextPane1.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
        jTextPane1KeyPressed(evt);
    }
});

private void jTextPane1KeyPressed(java.awt.event.KeyEvent evt) {
    // TODO add your handling code here:
    String txt = jTextPane1.getText();

}

Netbeans.

, - :

StyledDocument doc = jTextPane1.getStyledDocument();
Style style = jTextPane1.addStyle("Bold", null);
StyleConstants.setBold(style, true);

int pos, endpos;
// Find pos of word to be displayed in bold
...

// Set style
doc.setCharacterAttributes(pos, endpos, style, true);
0
source

Use RSyntaxTextArea , it does what you need and you can add your own lexers / rules.

0
source

All Articles