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);
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.