Can I highlight some text into a JTextPane
starting from a value and ending from another value
like the following but with the yellow color?
"" JTextPane highlight text ""
Thanks.
Can I highlight some text into a JTextPane
starting from a value and ending from another value
like the following but with the yellow color?
"" JTextPane highlight text ""
Thanks.
As often there are several possibilities, depending on what you really mean by "highlight":-)
Highlight by changing any style attributes of arbitrary text parts on the document level, something like
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, Color.YELLOW);
doc.setCharacterAttributes(start, length, sas, false);
Highlight via a Highlighter on the textPane level:
DefaultHighlighter.DefaultHighlightPainter highlightPainter =
new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
textPane.getHighlighter().addHighlight(startPos, endPos,
highlightPainter);
JTextArea textComp = new JTextArea();
// Highlight the occurrences of the word "public"
highlight(textComp, "public");
// Creates highlights around all occurrences of pattern in textComp
public void highlight(JTextComponent textComp, String pattern)
{
// First remove all old highlights
removeHighlights(textComp);
try
{
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
// Search for pattern
// see I have updated now its not case sensitive
while ((pos = text.toUpperCase().indexOf(pattern.toUpperCase(), pos)) >= 0)
{
// Create highlighter using private painter and apply around pattern
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
pos += pattern.length();
}
} catch (BadLocationException e) {
}
}
// Removes only our private highlights
public void removeHighlights(JTextComponent textComp)
{
Highlighter hilite = textComp.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0; i<hilites.length; i++)
{
if (hilites[i].getPainter() instanceof MyHighlightPainter)
{
hilite.removeHighlight(hilites[i]);
}
}
}
// An instance of the private subclass of the default highlight painter
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
// A private subclass of the default highlight painter
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter
{
public MyHighlightPainter(Color color)
{
super(color);
}
}
Yes you can via the functions setSelectionStart and setSelectionEnd from JTextComponent which JTextPane inherits from.
Did you try java's string comparison method
.equalsIgnoreCase("Search Target Text")
Because this method allows a search without having to take the case of a string into account This might be the ticket to what you are trying to achieve
Hope this helps you Makky
performance wise its better to put the toUpperCase on
String text = doc.getText(0, doc.getLength());
rather than in the while loop
but thanks for the good example.
© 2022 - 2024 — McMap. All rights reserved.