• Post Reply Bookmark Topic Watch Topic
  • New Topic

JText Component Question  RSS feed

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm working on making a small chat client. The area where you display different user messages I originally had as a JTextArea. I realized that (to my knowledge), I can't change the color of specific Strings in the component without changing the color of all the text. Such as Usernames, specific words, etc.

A little research shows that JTextPane allows you to modify Strings apart from others. However, it doesn't have the append() method from JTextArea which allows me to add text directly after that last line. I didn't see that JTextPane inherited any type of method such as that.

If someone could give me some pointers on how add messages one after another without an append() method in JTextPane?
Or maybe there's something I'm missing? Completely different text component that I can use?

Thanks guys.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use getDocument() to get the document, the default is a DefaultStyledDocument. then see its inherited insertString() method (from AbstractDocument)
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this behaves the same as a JTextPane or a Swing component, I can add a ScrollPane to it? Where I can modify Strings? Can be directly added to a JPanel?

I see insertString(offset, string, attribute), is the attribute parameter where I would insert something like Color.BLUE? The offset is simply the location?
 
Paul Clapham
Sheriff
Posts: 22513
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing you haven't read the tutorial. It's not going to be easy learning about a complex tool like JTextPane by asking random questions on a forum. I strongly recommend reading the tutorial and playing around with the examples it provides to see what you can make them do. That's how I learn about Java features which I haven't run into before.

Tutorial: How to Use Editor Panes and Text Panes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!