Hi!
I try to program a JTextPane that is to hold two different kinds of
lines. The first should be colored green and contain OK-messages; the
second should be colored red and contain error-messages. To achieve
this I have to operate on the text that is displayed in the JTextPane.
To append some text I use the text's insertString-method that takes a
new kind of style as an argument. I got stuck trying this:
try {
StyleContext styleContext = new StyleContext();
StyleContext.NamedStyle namedStyle = new
StyleContext.NamedStyle();
sqlOutput.getDocument().insertString(endPosition, newText,
namedStyle);
}
catch (javax.swing.text.BadLocationException e) {
System.err.println(e);
System.err.flush();
}
It seems to be a general mistake in object-oriented coding of mine for
I get the error-message from the compiler:
an enclosing instance that contains
javax.swing.text.StyleContext.NamedStyle is required
Why shall there be an enclosing instance of the NamedStyle for that is
the one to be created? And if the compiler does not complain about
NamedStyle, how can I tell him that there is a StyleContext named
styleContext as an instance that the compiler seems to miss.
Any help would be great! Kind regards,
Christian
Christian - 28 Jun 2006 16:38 GMT
I got a bit further and found a posting
[http://groups.google.de/group/comp.lang.java.gui/browse_frm/thread/fc216d285c0b5
a19/db7e77e2ade550ea?lnk=st&q=an+enclosing+instance+contains+is+required&rnum=8&
hl=de#db7e77e2ade550ea]
that seems to adress my problem. But I still do not manage to adapt and
make the compiler (1.4.2_08) agree with this
StyleContext outer = new StyleContext();
// third point in the following line is rejected with ^ ( expected
StyleContext.NamedStyle inner = outer.new StyleContext.NamedStyle();
By now the problem seems to have turned into a syntax-error, the
compiler complains about the point seperating StyleContext.NamedStyle()
- but as I got it this points indicates: the inner class NamedStyle of
StyleContext is to be instantiated and so the point seems essential to
me. I tried quiet some possible combination of brackets - does anybody
see, what I still miss in this?
Christian
Rogan Dawes - 28 Jun 2006 17:59 GMT
> I got a bit further and found a posting
> [http://groups.google.de/group/comp.lang.java.gui/browse_frm/thread/fc216d285c0b5
a19/db7e77e2ade550ea?lnk=st&q=an+enclosing+instance+contains+is+required&rnum=8&
hl=de#db7e77e2ade550ea]
[quoted text clipped - 13 lines]
>
> Christian
Try something like:
StyleContext.NamedStyle inner = outer.new NamedStyle();
You are already in the context of StyleContext because of outer.
Rogan