35

In an every article the answer to a question "How to append a string to a JEditorPane?" is something like

jep.setText(jep.getText + "new string");

I have tried this:

jep.setText("<b>Termination time : </b>" + 
                        CriterionFunction.estimateIndividual_top(individual) + " </br>");
jep.setText(jep.getText() + "Processes' distribution: </br>");

And as a result I got "Termination time : 1000" without "Processes' distribution:"

Why did this happen???

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Dmitry
  • 2,878
  • 5
  • 41
  • 63

3 Answers3

68

I doubt that is the recommended approach for appending text. This means every time you change some text you need to reparse the entire document. The reason people may do this is because the don't understand how to use a JEditorPane. That includes me.

I much prefer using a JTextPane and then using attributes. A simple example might be something like:

JTextPane textPane = new JTextPane();
textPane.setText( "original text" );
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);

//  Add some text

try
{
    doc.insertString(0, "Start of text\n", null );
    doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
}
catch(Exception e) { System.out.println(e); }
Eng.Fouad
  • 107,075
  • 62
  • 298
  • 390
camickr
  • 308,339
  • 18
  • 152
  • 272
  • 3
    That would recreate the Document and lose all the custom attributes you added previously. – camickr Nov 04 '10 at 15:21
  • 2
    @Dmitry Combining setText + getText to append is probably considered sloppy programming. (I personally use it for simple testing.) For instance, if I was going to maintain a log file with that method, a new String would have to be built every time a log entry is appended (which is a very bad idea given that Strings are immutable.) This will likely eventually lead to a noticeable memory footprint. – b1nary.atr0phy May 17 '12 at 23:18
  • 2
    Good example, but doesn't answer the question of appending text to a JEditorPane. – brianmearns Feb 06 '13 at 02:50
  • one might wana use [`HTMLDocument`](http://stackoverflow.com/questions/27512610/jtextpane-append-html-string) instead of `StyledDocument` in order to keep HTML-formatting, if used. – phil294 Dec 20 '14 at 18:18
27

A JEditorPane, just a like a JTextPane has a Document that you can use for inserting strings.

What you'll want to do to append text into a JEditorPane is this snippet:

JEditorPane pane = new JEditorPane();
/* ... Other stuff ... */
public void append(String s) {
   try {
      Document doc = pane.getDocument();
      doc.insertString(doc.getLength(), s, null);
   } catch(BadLocationException exc) {
      exc.printStackTrace();
   }
}

I tested this and it worked fine for me. The doc.getLength() is where you want to insert the string, obviously with this line you would be adding it to the end of the text.

Brandon Buck
  • 6,966
  • 2
  • 29
  • 47
  • I can't answer for sure, I haven't played around with JEditorPane at all, just JTextPane mostly. I'd have to play around and research it before I could answer that. – Brandon Buck Nov 04 '10 at 20:12
  • I think the issue is just that `setText` doesn't actually cause the component to update itself, for whatever reason it only works before the component is displayed. – brianmearns Feb 06 '13 at 02:54
  • @sh1ftst0rm I've never had issues with it before, but then again when I wrote a console emulator I didn't depend on setText I went with document manipulation for control. – Brandon Buck Feb 06 '13 at 22:01
  • @Dmitry Actually `setText(getText() + newText)` works, but its use is limited to no styled text. For styled text it will lose the previous style, and normally you don't want it. Besides, I found and submitted [a bug of JTextPane `getText()` till Java 9](http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8180276), so I will not rely on it. – WesternGun Jul 11 '17 at 13:26
4

setText is to set all text in a textpane. Use the StyledDocument interface to append, remove, ans so on text.

txtPane.getStyledDocument().insertString(
  offsetWhereYouWant, "text you want", attributesYouHope);
Istao
  • 7,015
  • 6
  • 30
  • 39