How to change the color of text between two positions?

swag antiswag

I want to change the color of my text for my Swing app (JTextPane) from two points, when highlighted. If the user highlights a phrase from, say, index 4 to 9, then only those characters will change their colors, permanently. I say permanently because I already know there is a setSelectionColor() option, but that is only temporary. I have managed to get the starting and ending points of the highlighted text, but I've reached a dead end.

Here is what I have so far:

StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet attributes = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color);

    if(tp.getSelectedText() != null){//tp is a jtextpane. text is highlighted. change highlighted text color
        int start = tp.getSelectionStart();
        int end = tp.getSelectionEnd();
        //update the color of the text within start and end
    }
tp.setCharacterAttributes(attributes, false);//update the color for the new text
camickr

I have managed to get the starting and ending points of the highlighted text,

You can set the attributes of the text with something like:

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setUnderline(keyWord, Boolean.TRUE );
StyleConstants.setBold(keyWord, true);

//  Change attributes on some text

StyledDocument doc = textPane.getStyledDocument();
doc.setCharacterAttributes(start, end - start, keyWord, false);

You can also use a StyledEditorKit Action to stylize text (bold, italic, color...). Read the section from the Swing tutorial on Text Component Features for more information and working examples.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to toggle between two buttons and change color using angular

From Dev

How to make a switch change between two sets of text?

From Dev

Switch on/off text color between two elements

From Dev

How to change spinner text color

From Dev

how to change text color hover

From Dev

How to change the text color of SlidingTabLayout?

From Dev

How to change color text in button?

From Dev

How to change Tkinter text color

From Dev

SearchBar, how to change text color?

From Dev

How to change the color of menu text?

From Dev

How to change color text in button?

From Dev

How to change color of copied text

From Dev

How to change the text color of a flatbutton?

From Dev

How to change change text and arrow color on Toolbar?

From Java

How to change spinner text size and text color?

From Dev

How to change the text color of all text in UIView?

From Dev

Plot cells between two positions

From Dev

Getting path between two positions

From Dev

Plot cells between two positions

From Dev

Change a text between two strings in Python with Regex

From Dev

Change everything in between two text/strings

From Dev

Java change text between two substrings

From Dev

How to change the color of two elements using javascript?

From Dev

Search lines in text file for pattern between two positions and print entire row

From Java

How to change the text color of the button theme in Flutter

From Dev

How to change the text color in a CATextLayer in Swift

From Dev

How to change the Action bar tab text color

From Dev

how to change the wordpress color picker button text?

From Java

How to change color for links (href text) in thymeleaf?

Related Related

HotTag

Archive