-3

I'm writing a script for a code editor and I want dynamic commands.

So, if the user types "class" it will change the colour of "class".

How do I do this?

// This is the main focus part of the code.

textarea.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            word += evt.getKeyChar();

            if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
                word = "";
                line = "";
                lineInMemory = line;
            }

            if(evt.getKeyCode() == KeyEvent.VK_SPACE) {
                word = word.replaceAll("null","");
                line += word;
                word = "";
                String text = textarea.getText();
                String[] words = line.split(" ");

                if(word.toLowerCase().equals("class")) {

                    // What the heck do I put here?!

                }       
            }
         }
    });

I already have key listeners that read the keys, put them into words, and then the words are put into sentences. I would like it so that they type the keyword and it automatically changes the colour of the keyword while they are still typing, a bit like what Sublime Text does.

mgthomas99
  • 5,402
  • 3
  • 19
  • 21
  • Welcome to StackOverflow, we are not a code factory as you might see. You should post your code or at least a [minimal example](http://stackoverflow.com/help/mcve) so we can copy-paste it and help you. If you don't show any effort in solving it by your own then I don't think anyone is gonna try to help you with this. – Frakcool Aug 29 '14 at 17:16
  • Yeah, I was editing the article as you posted it. Thanks! – mgthomas99 Aug 29 '14 at 17:21

1 Answers1

1

A JTextArea is only meant to contain plain text and cannot color certain words. If you want to be able to color different words, you need to use a JTextPane or a JEditorPane.

For more information, see this question. This question might also be helpful (especially the second answer).

Here is an example:

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

Style style = textPane.addStyle("Style", null);
StyleConstants.setForeground(style, Color.red);
String word = "Hello";

if (word.equals("Hello")) {
    try {
        doc.insertString(doc.getLength(), word, style);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
} else {
    StyleConstants.setForeground(style, Color.blue);

    try {
        doc.insertString(doc.getLength(), word, style);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

This makes a String word. If word is "Hello" it will be displayed in red, otherwise it will be displayed in blue.

Community
  • 1
  • 1
Zach
  • 4,652
  • 18
  • 22
  • Thanks for that, but it seems to be throwing an error. The error appears at line 5 of the code you submitted, where it says "StyleConstants.setForeground(style, Color.red);" and I'm not sure why. I'm probably missing something obvious, but I am relatively new to GUI programming. Any suggestions? – mgthomas99 Aug 29 '14 at 18:08
  • Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token(s), misplaced construct(s) Syntax error on token "(", delete this token Syntax error on token ")", invalid VariableDeclarator – mgthomas99 Aug 29 '14 at 19:23
  • Well, I found one error: There was a missing `)` on the line `if (word.equals("Hello")*)*`. I've fixed it now, but I'm not getting any other errors. Make sure all of your imports are correct. – Zach Aug 29 '14 at 19:28
  • You should have a look at the second answer here: http://stackoverflow.com/questions/14400946/how-to-change-the-color-of-specific-words-in-a-jtextpane Its probably what you are trying to do. – Zach Aug 29 '14 at 19:33