Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- StyledDocument doc = textPanel.getStyledDocument();
- Style style = textPanel.addStyle("Hightlight background", null);
- StyleConstants.setBackground(style, Color.red);
- Style logicalStyle = textPanel.getLogicalStyle();
- doc.setParagraphAttributes(textPanel.getSelectionStart(), 1, textPanel.getStyle("Hightlight background"), true);
- textPanel.setLogicalStyle(logicalStyle);
- Highlighter h = textPanel.getHighlighter();
- h.addHighlight(1, 10, new DefaultHighlighter.DefaultHighlightPainter(
- Color.red));
- doc.insertString(0, "Hello World", textPanel.getStyle("Hightlight background"));
- SimpleAttributeSet background = new SimpleAttributeSet();
- StyleConstants.setBackground(background, Color.RED);
- doc.setParagraphAttributes(0, doc.getLength(), background, false);
- doc.insertString(doc.getLength(), "nEnd of text", background );
- //choose color from JColorchooser
- Color color = colorChooser.getColor();
- //starting position of selected Text
- int start = textPane.getSelectedStart();
- // end position of the selected Text
- int end = textPane.getSelectionEnd();
- // style document of text pane where we change the background of the text
- StyledDocument style = textPane.getStyledDocument();
- // this old attribute set of selected Text;
- AttributeSet oldSet = style.getCharacterElement(end-1).getAttributes();
- // style context for creating new attribute set.
- StyleContext sc = StyleContext.getDefaultStyleContext();
- // new attribute set with new background color
- AttributeSet s = sc.addAttribute(oldSet, StyleConstants.Background, color);
- // set the Attribute set in the selected text
- style.setCharacterAttributes(start, end- start, s, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement