-1

I've seen several posts on this, but none seem to work.

I have a thread on the Java Programming Forums about this, please help!: http://www.javaprogrammingforums.com/whats-wrong-my-code/47440-trying-make-simple-java-editor-having-trouble-changing-colour-words.html

diggity
  • 118
  • 7

1 Answers1

0
DefaultStyledDocument document = new DefaultStyledDocument();
JTextPane textpane = new JTextPane(document);
StyleContext context = new StyleContext();
// build a style
Style style = context.addStyle("test", null);
// set some style color
StyleConstants.setForeground(style, Color.RED);
// add some data to the document
document.insertString(0, "", style);


OR




JTextPane pane = new JTextPane();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.red);
Document doc = pane.getStyledDocument();
doc.insertString(doc.getLength(), "Kleine ", set);
PeaceIsPearl
  • 329
  • 2
  • 6
  • 19
  • I'm accessing the doc.insertString part in a different function then everything else, and I'm getting a NullPointerException on it. I had it like this to debug: doc.insertString(0, "", style); with each of the different parts on a new line, and it had it on the function itself. – diggity May 21 '16 at 00:47
  • why do you need doc in document.insertString(0, "", style); in different function ? – PeaceIsPearl May 21 '16 at 01:11
  • i have tried one more option, edited my answer please check – PeaceIsPearl May 21 '16 at 01:16
  • Because I have a method that detects Java things like 'if' and 'for'. And that other one doesnt work either. – diggity May 21 '16 at 03:51