• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write each line with different font style in Jtextarea or JTextpane in Jframe?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,

I am stuck with the task to have different font style in each text line of Jtextarea inside JFrame.
How to do that?
I searched it from the internet also but didn't get any solution yet.
Please help me to get any solution.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recommended reading: https://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
 
SunilK Chauhan
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya i got it.

Here using JTextpane, we can use different style fonts in one pane only. But i think we can't use different font family for one JTextpane right?

We have to use only one Font family for the whole one JTextpane.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We have to use only one Font family for the whole one JTextpane.



No, you can use any font or style that you want.

Maybe this link will help more: https://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JTextArea was designed for all of its text to be displayed in a single style, so that's not what you want.

JTextPane, on the other hand, can fairly easily do what you want. Do something like this

where you should replace the "..." bits with whatever you actually want to do.

If you haven't seen AttributeSet before, it's in the javax.swing.text package and it's where you specify text style information. So the first time through the loop you could provide an AttributeSet with an italic 12-point font with red foreground color, for example, and the second time through you could provide an AttributeSet with a plain 18-point font with blue foreground color, etc.

It's permitted to pass null for the AttributeSet, but that wouldn't get you to your goal of presenting the JTextPane's lines in different styles.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic