Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JText Component Question

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic