whyn0t
whyn0t

Reputation: 301

displaying a styled content in a JTextArea

I was wondering if there is a way to style text in a JTextArea;

i have to insert lines of text that each one contains segments that might have different styles and type of content; type of content would determine the actions available to the segment they are related to.

could it be done another way? any advice to enhance the performance ? thank you.

Upvotes: 0

Views: 529

Answers (1)

camickr
camickr

Reputation: 324118

I examined JTextPane and JEditorPane and found them too complicated

Yes, well that is the way programming works. If you want to do something more complicated then you need to use a more advanced component.

A JTextArea doesn't support styled text which is why it is easy to use. A JTextPane does support styled text so it is a "little" more complicated.

A JTextPane really isn't all that complicated once you create a few styles to use for each group of text. For example:

JText textPane = new JTextPane();
textPane.setText( "This is regular text"");
StyledDocument doc = textPane.getStyledDocument();

//  Add some styled text

SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);

try
{
    doc.insertString(doc.getLength(), "\nThis is green text.", green);
}
catch(Exception e) { System.out.println(e); }

Upvotes: 2

Related Questions