TextArea and different colors? is it possible?

Hi!I wanna write a TextArea which lines can have different colors. Can anyone say me how i can do this? Do i need a StyledDocument?thxmatthias
[177 byte] By [schranzelot] at [2007-9-26 12:21:42]
# 1
Hi,you are on the right way - but not with JTextArea - use JTextPane with a DefaultStyledDocument in it, that is capable of multi-font and multi-color text.greetings Marsian
marsian27 at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...
# 2
thx, but, this means i must take swing components? with awt i have no chance? and, another question :) how can i create styles, for example how can i add a word which is bold? matthias
schranzelot at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...
# 3

Hi again,

you can use SimpleAttributeSet to specify the textattributes like font and colors. To append the document, you get first the document by the getStyledDocument()-method of the JTextPane and then append it by using the insertString-method of the document with postion, text and your attributeset.

Hope this will help you

greetings Marsian

P.S.: Don't know much about the heavy-weight non-swing-components, I normally use swing.

marsian27 at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...
# 4
Oh ok, appending new text is easy, but creating a AttributeSet not, because i started creating one about thousand times..but i never created one who did something :)so can you say it to me? i can post a new topic if i ask you to much...
schranzelot at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...
# 5

ok, here is one example - it sets some Attrbutes in an array of 17 SimpleAttributeSets - this array is declared outside this method as SimpleAttributeset [] attr = new SimpleAttributeSet[17];

public void initAttrSet(int fontsize) {

attr[0]=new SimpleAttributeSet();

StyleConstants.setFontFamily(attr[0],"Courier New");

StyleConstants.setFontSize(attr[0],fontsize);

StyleConstants.setForeground(attr[0],Color.lightGray);

StyleConstants.setBackground(attr[0],Color.black);

attr[1]=new SimpleAttributeSet(attr[0]);

StyleConstants.setForeground(attr[1],Color.red);

attr[2]=new SimpleAttributeSet(attr[0]);

StyleConstants.setForeground(attr[2],Color.green);

attr[3]=new SimpleAttributeSet(attr[0]);

StyleConstants.setForeground(attr[3],Color.yellow);

// and so on ....

}

Hope this will help now

greetings Marsian

P.S.: if you wonder, why the background is black - I use it in a mud-client for a science fiction mud, there black background is more atmospheric :)

marsian27 at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...
# 6
:) whats the name of this mud? im a programmer in the mud sinn?. we just started programming so we have not much...are you good in lpc?ok, but, thanx thanx and thanx :)
schranzelot at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...
# 7

Hi again,

it is a german mud called finalfrontier - but I am not a Q, I am player - I just wrote my own client with integrated programming language, JIT-Compiler, virtual machine and such things. In finalfrontier it is forbiddden to use scripts and automatic triggers, so I had to find an other way to use this things in a mud-conform manner. I can't use the features of zmud there, so I had to do it my own way.

greetings Marsian

P.S.: if you will visit this mud, you will find me in the genetic guild - I am DNA-master of the biogenetic institute in Rat~Leihfi on planet Ch~Rihan in the Taladon system - ofcourse, as Marsian

marsian27 at 2007-7-2 3:00:05 > top of Java-index,Archived Forums,Java Programming...