setForeground doesn't work on a Style(dDocument)

import java.awt.*;

import java.util.*;

import javax.swing.*;

import javax.swing.text.*;

publicclass Documentextends JApplet

{

private JTextPane pane;

private JScrollPane scroller;

private StyledDocument document;

public Document()

{

pane =new JTextPane();

pane.setEnabled(false);

scroller =new JScrollPane(pane);

scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

add(scroller, BorderLayout.CENTER);

document = pane.getStyledDocument();

String text ="Hey what's up?";

Color color = Color.black;//Black right?

addText(text, color);

}

publicvoid addText(String text, Color color)

{

try

{

Style style = pane.addStyle("talk",null);

StyleConstants.setForeground(style, color);//DOESN'T WORK, but setBackground does work...weird...

StyleConstants.setFontFamily(style,"Verdana");

StyleConstants.setFontSize(style, 12);

StyleConstants.setBold(style,true);

StringTokenizer st =new StringTokenizer(text);

while(st.hasMoreTokens())

{

String word = st.nextToken();

document.insertString(document.getLength()," " + word, style);

}

document.insertString(document.getLength(),"\n", style);

}

catch(BadLocationException e)

{

e.printStackTrace();

}

}

}

Instead of using Style I also used MutableAttributeSet and SimpleAttributeSet...it all doesn't work.

Somebody knows why?

[2887 byte] By [Dennis26a] at [2007-10-3 8:56:25]
# 1

Hmm this is not good...

Compiling/running under 1.3:

- good foreground color

- not bold text

Compiling/running under 1.4:

-Everything looks fine :)

Compiling/running under 1.5:

- wrong foreground color

- bold text

This means it is not possible to use styles in a textpane, because the result is unpredictable?

Or can it work with a little rewrite of the code?

Dennis26a at 2007-7-15 4:06:41 > top of Java-index,Java Essentials,Java Programming...
# 2

Swing related questions should be posted in the Swing forum.

This posting contains my example for using attributes that is executable and works fine on 1.4.2:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=342068

Maybe if you actually posted executable code, people would try your code to see if it works on their machine and JDK.

You should also give your class a better name. Document is part of the JDK.

camickra at 2007-7-15 4:06:41 > top of Java-index,Java Essentials,Java Programming...
# 3
Reposted: http://forum.java.sun.com/thread.jspa?messageID=4451575Of course the OP still missed the point about posting executable code.
camickra at 2007-7-15 4:06:41 > top of Java-index,Java Essentials,Java Programming...