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
⌛
This post has been reserved for your question.
Hey @blockgoblin31! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
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
So you can use
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?
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...
no, I was mostly working from this https://stackoverflow.com/questions/9650992/how-to-change-text-color-in-the-jtextarea
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...
I just cant figure out what that's doing to add more text
and what you linked is using the same code pretty much
This bit at the bottom:
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
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
s is just a wrapper for a Color and a char
set is an AttributeSet where im just changing out the color
actually heres the full class
and here's Symbol.java
@imp_o_rt
💤
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.
bump
💤
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.