0

I am trying to make an 'advanced text editor' in Java. However, I can't really find a way to search for certain words in a TextArea and set the font/color of only the word or words it finds. (Kind of like how Eclipse has a pinkish color for 'import', but blue for variable names.

Here is the source of what I have so far: http://pastebin.com/1fKBUCWY

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
None None
  • 147
  • 2
  • 3
  • 11
  • 1
    What you're looking can't be done, completely. What you can do is cheat. Take a look at [**this**](http://stackoverflow.com/questions/13437865/java-scroll-to-specific-text-inside-jtextarea/13438455#13438455) example. It basically selects the text as it matches each occurance. If you want highlighting, you'll need to look towards using a `JTextPane`, which can be accomplished in a similar manner to [**this**](http://stackoverflow.com/questions/13448558/highlight-a-word-in-jeditorpane/13449000#13449000) – MadProgrammer Jun 27 '13 at 05:11
  • textarea.getText().indexOf("thestring")?? – fmodos Jun 27 '13 at 05:12
  • @fmodos Now get the next match ;) – MadProgrammer Jun 27 '13 at 05:43
  • Or even [**this**](http://stackoverflow.com/questions/14727548/java-change-the-document-in-documentlistener/14727657#14727657) – MadProgrammer Jun 27 '13 at 05:47
  • 1
    @MadProgrammer `string.substring(idxMatch1+"searched".length()).indexOf('...')` lol... I missed the plural in the 'word' :/ – fmodos Jun 27 '13 at 05:57
  • See [this](http://stackoverflow.com/questions/14229611/code-completion-and-syntax-highlighting-in-swing/14230233#14230233) similar answer. Baically shows multiple examples which together would make code completion and syntax highlighting app. Specifically though to highlight letter(s) see [here](http://stackoverflow.com/questions/13074428/how-can-i-set-each-character-to-a-different-color-background-color-in-a-jtextpan/13076649#13076649) and [here](http://stackoverflow.com/a/12482171/1133011) depending on which you need. – David Kroukamp Jun 27 '13 at 11:56

1 Answers1

0

This is how I would do it.

First set up a listener on the TextArea that listens for the Space character. Store the word (between last event and this event) in a local variable and compare it with your special-words-list.

Responses to text-area event handling can be found at Text Changed event in JTextArea? How to?

Hope this helps!

Community
  • 1
  • 1
user1349663
  • 595
  • 1
  • 7
  • 21