Better use a JTextPane or JEditorPane instead of a JTextArea .
The text area is a โsimpleโ text component, which means taht, although it can display text in any font, all text has the same font.
So, JTextArea not a convenient component for formatting text.
Conversely, using JTextPane or JEditorPane , itโs quite easy to change the style (selection) of any part of the loaded text.
For more information, see How to Use Editing Panels and Text Panels .
Update:
The following code highlights the right part of your text. This is not what you want. He simply finds the exact phrase in the text.
But I hope that if you apply your algorithms, you can easily change it to suit your needs.
import java.lang.reflect.InvocationTargetException; import javax.swing.*; import javax.swing.text.*; import java.awt.*; public class LineHighlightPainter { String revisedText = "Extreme programming is one approach " + "of agile software development which emphasizes on frequent" + " releases in short development cycles which are called " + "time boxes. This result in reducing the costs spend for " + "changes, by having multiple short development cycles, " + "rather than one long one. Extreme programming includes " + "pair-wise programming (for code review, unit testing). " + "Also it avoids implementing features which are not included " + "in the current time box, so the schedule creep can be minimized. "; String token = "Extreme programming includes pair-wise programming"; public static void main(String args[]) { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { new LineHighlightPainter().createAndShowGUI(); } }); } catch (InterruptedException ex) {
This example is based on Highlighting words in a JTextComponent .
source share