• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
SunilK Chauhan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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.
 
SunilK Chauhan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • 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
 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!