Change specific String attributes in StyledDocument
Asked Answered
Q

3

4

I am trying to create a text editor. I am using a JTextPane with a StyledDocument. What I am trying to implement is a way to change the attributes of selected text.

This works in the following way : the user inputs the desired text. Afterwards, he can change any String's attributes (Font family, font size, whether it is bold/italic or not) by selecting it and pressing a button, where by means of checkboxes and dropdown lists would select the desired changes.

Is it possible for me to change that selected String's attributes without the need to rebuild the document? I have been searching but was unable to find a proper solution.

Queri answered 7/1, 2013 at 15:22 Comment(0)
B
3

You can use the actions provided by the StyledEditorKit, seen here and discussed in How to Use Editor Panes and Text Panes.

image

Bearskin answered 7/1, 2013 at 19:30 Comment(0)
H
4

You would use the setCharacterAttributes method of StyledDocument.

Here's an example from one of my Swing applications, that highlights the text with a highlight color.

        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(
                SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, highlightColor);
        cobolProgram.setCharacterAttributes(offset, length, aset,
                false);

You can use other StyleConstants to change other style attributes.

Holle answered 7/1, 2013 at 16:0 Comment(0)
B
3

You can use the actions provided by the StyledEditorKit, seen here and discussed in How to Use Editor Panes and Text Panes.

image

Bearskin answered 7/1, 2013 at 19:30 Comment(0)
C
1

In my case, I "cleaned" the style when the user changes the text:

StyledDocument doc = tf.getStyledDocument();

//clean style
doc.setCharacterAttributes(0, sb.length(), DEF, true); 
Chauncey answered 7/1, 2013 at 15:59 Comment(1)
Please, take a look: #8160052Chauncey

© 2022 - 2024 — McMap. All rights reserved.