For Programmers: Free Programming Magazines  


Home > Archive > Java Help > December 2004 > Help: Attributes are ignored









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help: Attributes are ignored
Peter Theissen

2004-12-17, 8:57 am

Hi,

I want to print out a String with certain attributes. Therefore I´m
adding an StyledDocumentObject into an JTextArea.
Now "strange" things happen: the text is ("This is only a Test")
printed out on the screen but the attributes are ignored. I used the
getAttributeCount() method to count the attributes in the attribute
set and the number is as I expected.


The source code looks as follows:
--------------------------------------------------------------------
public class Test
{
protected JTextArea termArea;

termArea = new TextAreaCtl();
termArea.setEditable(false);

DefaultStyledDocument styledDoc = new DefaultStyledDocument();
SimpleAttributeSet mySimpleSet = new SimpleAttributeSet();
System.out.println("ANZATTR :"+ mySimpleSet.getAttributeCount());

StyleConstants.setBold(mySimpleSet, true);
StyleConstants.setForeground(mySimpleSet, Color.green);
System.out.println("ANZATTR :"+ mySimpleSet.getAttributeCount());

try
{
System.out.println("ANZATTR: "+ mySimpleSet.getAttributeCount());
styledDoc.insertString(0, "This is only a Test!", mySimpleSet);
}
catch(BadLocationException e) {System.out.println("Error");}

termArea.setDocument(styledDoc);
}



--------------------------------------------------------------------
The output is:

0
2
2
--------------------------------------------------------------------


Why are the changes in mySimpleSet ignored?

How can I overcome this?

kind regards,

Peter



Jim McMaster

2004-12-23, 9:04 pm

At 12/17/2004 3:26:56 AM, Peter Theissen wrote:

> Hi,
>
> I want to print out a String with certain attributes. Therefore I´m
> adding an StyledDocumentObject into an JTextArea.
> Now "strange" things happen: the text is ("This is only a Test")
> printed out on the screen but the attributes are ignored. I used the
> getAttributeCount() method to count the attributes in the attribute
> set and the number is as I expected.
>


A JTextArea cannot display text with attributes. From the javadoc for
JTextArea:

A JTextArea is a multi-line area that displays PLAIN TEXT. (emphasis
added).

You need to use a JEditorPane instead.

--
Jim McMaster
mailto:jim.mcmaster@comcast.net

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com