java - Highlighter addHighlight not changing text color -
i have jtextarea in highlight text using addhighlight method of highlighter jtextarea. highlights text not change text color of highlighted text selectedtextcolor have set.
here example:
import java.awt.color; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.swingutilities; import javax.swing.text.badlocationexception; import javax.swing.text.defaulthighlighter; import javax.swing.text.highlighter; import javax.swing.text.highlighter.highlightpainter; public class sscce { private jframe frame; private jtextarea textarea; public sscce() { frame = new jframe(); frame.settitle("huge text"); frame.setdefaultcloseoperation(jframe.exit_on_close); textarea = new jtextarea("abcd abcd abcd"); textarea.setbackground(color.dark_gray); textarea.setforeground(color.light_gray); textarea.setselectioncolor(color.light_gray); textarea.setselectedtextcolor(color.dark_gray); highlighter highlighter = textarea.gethighlighter(); highlightpainter highlightpainter = new defaulthighlighter.defaulthighlightpainter(textarea.getselectioncolor()); try { highlighter.addhighlight(0, 10, highlightpainter); } catch (badlocationexception e) { e.printstacktrace(); } frame.add(new jscrollpane(textarea)); frame.setsize(400, 350); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new sscce(); } }); } }
worth reading using text components
if intend use unstyled text component choose text field, password field, formatted text field, or text area.
if intend use styled text component, see how use editor panes , text panes
jtextarea
doesn't support functionality style sub set of entire content. applies styles across entire content.
find sample code here change specific text color in java
Comments
Post a Comment