user3077408
user3077408

Reputation: 11

How to append Text in JTextPane?

How to append Text in JTextPane like JTextArea? like

JTextPane pText1 = new JTextPane();
pText1.append( txt1.getText + "\n" );
pText1.append( txt2.getText + "\n" );
pText1.append( txt3.getText + "\n" );

Upvotes: 1

Views: 13883

Answers (2)

An_Bk
An_Bk

Reputation: 179

There is no append method in JTextPane objects. Most used, similar to "append":

JTextPane pText1 = new JTextPane();
pText1.setText(txt1.getText + "\n");
pText1.setText(pText1.getText() + txt2.getText + "\n");
pText1.setText(pText1.getText() + txt3.getText + "\n");

Upvotes: -2

Sage
Sage

Reputation: 15428

Well, JTextPane works with Document model such asStyledDocument to manage text data. Because JTextPane differs from JTextArea in the sense that, we use JTextPane for styling text. However, if you need to append-string feature for your own requirement, you can easily build your won appendString function by extending JTextPane to work with:

public void appendString(String str) throws BadLocationException
{
     StyledDocument document = (StyledDocument) jTextPane.getDocument();
     document.insertString(document.getLength(), str, null);
                                                    // ^ or your style attribute  
 }

The above function first ask the text pane to for the StyledDocument associating with it. Then it make use of insertString(int offset, String str, AttributeSet a) function.

Upvotes: 4

Related Questions