How do I append text to a JTextPane?

I've only used JTextAreas in the past, but my current project needs colored text, so I need to use JTextPane or JEditorPane. From googling JTextPane seems simpler but I still cant understand how I actually add text
15 Replies
JavaBot
JavaBot12mo ago
This post has been reserved for your question.
Hey @blockgoblin31! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
imp_o_rt
imp_o_rt12mo ago
jtextpane and jtextarea are both JTextComponents which have a getText/setText method: https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/JTextComponent.html#setText(java.lang.String)
JTextComponent (Java SE 17 & JDK 17)
declaration: module: java.desktop, package: javax.swing.text, class: JTextComponent
imp_o_rt
imp_o_rt12mo ago
So you can use
myPane.setText(myPane.getText() + " and some more text!")
myPane.setText(myPane.getText() + " and some more text!")
blockgoblin31
blockgoblin31OP12mo ago
but how would I use that to color specific parts of the text? wouldn't that color the whole thing then the text is set?
imp_o_rt
imp_o_rt12mo ago
Ah. I think in that case you will need to mess with the StyleContext - did you see this? https://stackoverflow.com/questions/15727836/is-it-possible-to-color-only-a-portion-of-jtextpane
Stack Overflow
Is it possible to color only a portion of JTextpane?
Hi I am trying to find out whether it is possible to apply background color only to a portion of JTextpane? For example if I have a JTextpane of size 300 X 400 then can I apply some color to a port...
blockgoblin31
blockgoblin31OP12mo ago
Stack Overflow
How to change text color in the JTextArea?
I need to know how to do this: Let's say: I have a code in the JTextArea like this: LOAD R1, 1 DEC R1 STORE M, R1 ADD R4, R1,8 I wanted to change the color of LOAD, DEC, STORE and ADD to color B...
blockgoblin31
blockgoblin31OP12mo ago
I just cant figure out what that's doing to add more text and what you linked is using the same code pretty much
imp_o_rt
imp_o_rt12mo ago
This bit at the bottom:
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
1. Sets the caret to the end (i.e. where you want to insert text) 2. Sets the requested attributes at that position 3. Writes the message at that position
blockgoblin31
blockgoblin31OP12mo ago
ok, I think that makes sense how do I set the default side of the window? in columns and rows also it isn't working using this code
set = context.addAttribute(set, StyleConstants.Foreground, s.getColor());
int length = getDocument().getLength();
setCaretPosition(length);
setCharacterAttributes(set, false);
replaceSelection(String.valueOf(s.getText()));
set = context.addAttribute(set, StyleConstants.Foreground, s.getColor());
int length = getDocument().getLength();
setCaretPosition(length);
setCharacterAttributes(set, false);
replaceSelection(String.valueOf(s.getText()));
s is just a wrapper for a Color and a char set is an AttributeSet where im just changing out the color
blockgoblin31
blockgoblin31OP12mo ago
actually heres the full class
blockgoblin31
blockgoblin31OP12mo ago
and here's Symbol.java
blockgoblin31
blockgoblin31OP12mo ago
@imp_o_rt
JavaBot
JavaBot12mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
blockgoblin31
blockgoblin31OP12mo ago
bump
JavaBot
JavaBot12mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?