Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTextPane changing text color issues

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default JTextPane changing text color issues

    Hi everyone, i'm new and I am struggling with a problem, please anyone help.

    I created a GUI using JTextPane and i've got it working, I have created a working trie tree and I am trying to implement a realtime spellchecker.

    What my program does according to my logic at the moment is if a word is not found in my trie, the word is changed to the color red.
    I know my trie is working because I system out the words that aren't in the TRIE and it does so correctly. Now when I go into the if statement for when a word isn't in the trie I try the setCharacterAttributes method on the word from its starting index to the end of the word. the indexes work because I output them too and they are correct, I counted them myself to make sure, here is my code:

    System.out.println("Misspelled words: ");
     int indexS = 0, indexE = 0;
     String all = createGui.ta.getText();
     SimpleAttributeSet attrs = new SimpleAttributeSet();;
     StyleConstants.setForeground(attrs, Color.red);
     StyledDocument doc = createGui.ta.getStyledDocument();
                   indexS = all.indexOf(s.toLowerCase());
                   System.out.println(s + " on line: " + lineNum);
                   indexE = indexS+s.length()-1;
                   System.out.println("indexS is at: " + indexS);
                   System.out.println("indexE is at: " + indexE);
                   doc.setCharacterAttributes(indexS,indexE, attrs, false);
                if(ch == -1)
    Oh and by the way this is when I open an existing text file.
    Here is the GUI and output I receive from netbeans:

    As you can see it isn't highlighting correctly, I'm not sure why, please help I've been struggling for hours.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: JTextPane changing text color issues

    This thread has been cross posted here:


    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How Do I Make Color Changing Buttons?
    By blurredvis0n in forum AWT / Java Swing
    Replies: 3
    Last Post: August 30th, 2011, 02:14 PM
  2. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  3. Need help with changing color of JOptionPane
    By jwise95 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2011, 09:49 AM
  4. Changing Panel Color with a ComboBox
    By bengregg in forum AWT / Java Swing
    Replies: 7
    Last Post: March 2nd, 2011, 10:04 PM
  5. changing the visited links color in JEditorpane
    By nasi in forum AWT / Java Swing
    Replies: 5
    Last Post: April 18th, 2010, 08:44 AM