0

So I was wondering how I can actively change the color of keywords in the java text pane. I understand a document listener is will have to be used, but at the moment it doesn't seem to be working, in fact putting it in the document listener leads to me not being able to properly open a file or color at all. So how can I actively call a method that changes color of keywords in java. This is the code that will search for keywords and it works when I open files, just not actively.

public void findKeyWords(String directory) throws FileNotFoundException
{
  final StyleContext cont = StyleContext.getDefaultStyleContext();
  final AttributeSet jKeyWord = cont.addAttribute(cont.getEmptySet(), 
     StyleConstants.Foreground,Color.RED);
  final AttributeSet jOperator = cont.addAttribute(cont.getEmptySet(), 
     StyleConstants.Foreground,Color.MAGENTA);
  final AttributeSet jtypes = cont.addAttribute(cont.getEmptySet(), 
     StyleConstants.Foreground,Color.CYAN);

     ArrayList<String> words = loadKeyWords(directory);
     for (String line : words)
     {
        searchJava(line,jKeyWord);
     }
     ArrayList<String> operators = loadOperators(directory);
     for (String line : operators)
     {
        searchJava(line, jOperator);
     }
     ArrayList<String> types1 = loadTypes(directory);
     for (String line : types1)
     {
        searchJava(line, jtypes);
     }
}
private ArrayList<String> loadKeyWords(String directory) throws FileNotFoundException
{
  ArrayList<String> javaWords = new ArrayList<String>();
  final String dir = System.getProperty("user.dir");
  File file = new File(dir + "/" + directory + "/keywords.txt");
  Scanner scan = new Scanner(file);
  while(scan.hasNext())
  {
     javaWords.add(scan.next() + " ");
  }
  scan.close();
  return javaWords;
}
private ArrayList<String> loadOperators(String directory) throws FileNotFoundException
{
  ArrayList<String> javaWords = new ArrayList<String>();
  final String dir = System.getProperty("user.dir");
  File file = new File(dir + "/" + directory + "/operators.txt");
  Scanner scan = new Scanner(file);
  while(scan.hasNext())
  {
     javaWords.add(scan.next());
  }
  scan.close();
  return javaWords;
}
private ArrayList<String> loadTypes(String directory) throws FileNotFoundException
{
  ArrayList<String> javaWords = new ArrayList<String>();
  final String dir = System.getProperty("user.dir");
  File file = new File(dir + "/" + directory + "/types.txt");
  Scanner scan = new Scanner(file);
  while(scan.hasNext())
  {
     javaWords.add(" " + scan.next());
  }
  scan.close();
  return javaWords;
}
 public void searchJava(String wordToSearch, AttributeSet javaAttr) 
{ 

  final AttributeSet attr = javaAttr;
  Document text = textArea.getDocument();

  int m;
  int t;
  int total = 0;
  for (String line : textArea.getText().split("\n")) 
  {
     m = line.indexOf(wordToSearch);
     if(m == -1)
     {
        if(isUnix())
        {
        total += line.length() + 1;
        }
        else if(isWindows())
        {
           total += line.length();
        }
        else if(isMac())
        {
           total += line.length() + 1;
        }
        else
        {
           total += line.length() + 1;
        }
        continue;
     }
     try{
     text.remove(total + m, wordToSearch.length());

     text.insertString(total + m, wordToSearch, attr);
     }catch(BadLocationException ex)
     {}
     while(true)
     {
        m = line.indexOf(wordToSearch, m + 1 );

        if (m == -1)
        {
           break;
        }
        try
        {
        text.remove(total + m, wordToSearch.length());

        text.insertString(total + m, wordToSearch, attr);
        }catch(BadLocationException e)
        {
        }
     }
     if(isUnix())
        {
           total += line.length() + 1;

        }
        else if(isWindows())
        {
           total += line.length();
        }
        else if(isMac())
        {
           total += line.length() + 1;
        }
        else
        {
           JOptionPane.showMessageDialog(null, "Eric You Troll" );
           total += line.length() + 1;
        }
  }

}

Abhinav Singh
  • 77
  • 1
  • 1
  • 4
  • For line number you may want to look at [Text Component Line Number](https://tips4java.wordpress.com/2009/05/23/text-component-line-number/) which you can use with a JTextArea or JTextPane. – camickr Mar 13 '16 at 04:09
  • Don't use the Quote button for code. Instead use the "{}" button to mark up the code. Syntax highlighting is not support in the base JDK. Check out this posting: http://stackoverflow.com/a/27338679/131872 for an example of you might do this in Java. It is complicated and doesn't work 100% especially for trying to handle comments. But theoretically you should be able to change the keywords for python keywords and you should get something. The code also uses a custom Document, but you should be able to move the code from insertString() and remove() to the DocumentListener methods. – camickr Mar 13 '16 at 04:13
  • 1
    For better solutions you need to search the forum/web using terms like "syntax highlighting". – camickr Mar 13 '16 at 04:14
  • [There's one way](http://stackoverflow.com/questions/14727548/java-change-the-document-in-documentlistener/14727657#14727657) and you can do line number lke [this](http://stackoverflow.com/questions/21766588/make-jscrollpane-control-multiple-components/21767752#21767752) – MadProgrammer Mar 13 '16 at 04:15
  • So, this is the third time you're asked this question and you've since failed to adhere to our try any of the suggestions/recommendations so far provided, we should we keep telling you the same thing? – MadProgrammer Mar 13 '16 at 04:18
  • Oh, well I thought I was asking a different type of question. My apologies for being repetitive. However, its just that these ideas are not working out too well. My document listener is working, but the method when put inside it doesn't behave as expected. – Abhinav Singh Mar 13 '16 at 07:25
  • *"..not working out too well."* For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (As opposed to code snippets.) Also be a lot more specific about what you mean by 'not working'. We are not likely to be able to help fix problems with that little detail. – Andrew Thompson Mar 13 '16 at 12:40

0 Answers0