Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: JTetPane changing text colour help

  1. #1
    donnierisk is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default JTetPane changing text colour help

    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:

    Java 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();
    
                   while(true)
                   {
                       readWord(fIn);
                       if(!trie.found(s))
                       {
                          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)
                           break;
                   }
    Oh and by the way this is when I open an existing text file.
    Here is the GUI and output I receive from netbeans:
    JTetPane changing text colour help-gui.pngJTetPane changing text colour help-output.png

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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: JTetPane changing text colour help

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. No spellcheck code, just a JTextPane populated with some text, and a button with an ActionListener that finds and highlights certain words in the text pane. Include your debug sysouts in the SSCCE.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: JTetPane changing text colour help

    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: JTetPane changing text colour help

    Thanks Kevin. I'm outta here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to underline text using a different colour
    By Jack_Maloney in forum AWT / Swing
    Replies: 3
    Last Post: 02-07-2012, 10:33 PM
  2. changing JFrame colour
    By simo_mon in forum AWT / Swing
    Replies: 0
    Last Post: 08-08-2009, 02:15 AM
  3. Changing text colour in code
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-29-2009, 03:32 PM
  4. Replies: 0
    Last Post: 03-26-2009, 01:23 AM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •