Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextPane appending a new string

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???

like image 468
Dmitry Avatar asked Oct 30 '10 14:10

Dmitry


People also ask

How do I add text to JTextPane?

To append text to the end of the JTextArea document we can use the append(String str) method. This method does nothing if the document is null or the string is null or empty.

How do I add text to JTextArea?

Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.

What is JEditorPane in Java?

public class JEditorPane extends JTextComponent. A text component to edit various kinds of content. You can find how-to information and examples of using editor panes in Using Text Components, a section in The Java Tutorial. This component uses implementations of the EditorKit to accomplish its behavior.

What is JTextPane?

A JTextPane is a subclass of JEditorPane. A JTextPane is used for a styled document with embedded images and components. A JTextPane is a text component that can be marked up with the attributes that are represented graphically and it can use a DefaultStyledDocument as the default model.


2 Answers

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); } 
like image 122
camickr Avatar answered Sep 20 '22 11:09

camickr


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.

like image 30
Brandon Buck Avatar answered Sep 21 '22 11:09

Brandon Buck