0

I am writing a code which gives different colors to key works.
I have written following code which will compare with key word say "ABC" when ever it finds "ABC" it will make it into blue color else red color.I am not getting whats wrong in my code.

private void jTextPane1KeyPressed(java.awt.event.KeyEvent evt) {                                      
        StyledDocument d = jTextPane1.getStyledDocument();
        MutableAttributeSet a = new SimpleAttributeSet();
    String sr,sr1;
    sr=jTextPane1.getText();
    int i,j,k=0,l,len=0;
    l=0;
    i=sr.length();
    while(k<jTextPane1.getText().length())
    {
        if(jTextPane1.getText().charAt(k)==' ')
        {
           len++;
        }
        k++;
    }
    do
    {
       j=sr.lastIndexOf(" ",i-1);
      if(j>=0)
      {
        k=j;
        sr1=sr.substring(j,i);
        if(sr1.compareToIgnoreCase("ABC")==0){
            StyleConstants.setForeground(a, Color.blue);
            d.setCharacterAttributes(j, i+1, a, false);
        }
       else
       {
         StyleConstants.setForeground(a, Color.red);
         d.setCharacterAttributes(j, i, a, false);
       }
        i=j;
        l++;
      }
      else
      {
           if(sr.substring(0,i).compareToIgnoreCase("ABC")==0){
            StyleConstants.setForeground(a, Color.blue);
            d.setCharacterAttributes(0, i+1, a, false);
        }
        else
        {
            StyleConstants.setForeground(a, Color.red);
            d.setCharacterAttributes(0, i, a, false);
        } 
          l++;
      }
    }
    while(l <= len);
}

Edited by Nick Evan: Fixed formatting

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ashwin Vasnai
0

I am working on Netbeans 6.7 Java DesktopApplication.
I want to know to get Name of element and Number which is clicked in Popupmenu.
Also on adding elements, MyActionListner of JPopupMenu is not getting invoke.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.