I working with a simple JTextField and I want to be able to highlight the text within it. I've studied the JTextComponent.getHighlighter() and Highlighter.addHighlight(int,int,Highlighter.HighlightPainter) methods. What I'd like to do is this:
You'll probably notice the problem: there is no such method as Highlighter.getHighlightPainter(). There doesn't seem to be anything that allows me to get access to the Highlighter's HighlightPainter, and I don't even know if the Highlighter returned from the call to TF.getHighlighter() even has an implemented HighlightPainter in which case I may have to program one myself. This wouldn't be a problem except that I'm not sure how to program the HighlightPainter's paint() method. What am I supposed to do with the parameters Graphics g, int p0, int p1, Shape bounds, and JTextComponent c? If I knew what to do, I could program a HighlightPainter and then do this:
JTextField TF = SomeJTextFieldWithTextInIt; MyHighlightPainter HP = new MyHighlightPainter(); TF.getHighlighter().addHighlight(0, TF.getText().length()-1, HP);
Do I have to program my own HighlightPainter, and if so, how do I program the paint() method? If the Highlighter I get from TF.getHighlighter() already has an instance of HighlightPainter within it, how do I gain access to it?
I was looking for the same. And I don't have time to make it by my own. So I look for it and I can tell you two choices: http://ostermiller.org/syntax/ have an open source application with several syntax highlighting but there is something wrong in it: it may fall in deadlock The second choice (the best one) is to make a plugin for Eclipse, Eclipse is free and has Tutorials an samples about syntax highlighting, but I can tell you it isn't as easy as they say (because they don't guide a novice enought). I think that is the ideal solution, because I like their hierarchy and reusability.