Reputation: 91
I am trying to create a text editor using a JTextPane that outputs the text as HTML. I want to include the options to bold, color and align the text. I am having an issue with changing the color of a selection of text that is a mix of bold and not bold. For example, if I have the text in the editor "bold text not bold text" and I highlight both words and try to change the color of the text, it changes the color and makes all of the text bold, instead of leaving the second word not bold. So I'm not sure where this issue is coming from, if it has to do with setCharacterAttributes or if it's a problem with outputing the text as html. here is some of my code that makes the text bold:
MutableAttributeSet attrs = pane.getInputAttributes();
StyleConstants.setBold(attrs, bold);
pane.getStyledDocument().setCharacterAttributes(p.getSelectionStart(),len,attrs,false);
Here is an example of what the html output looks like before and after I change the color of the text. This is just whats inside of the body tags
Before: bold text not bold text
After change color to red: bold text not bold text
And it has something to do with whether or not the first word of the selection is bold. If I did the same example, but the 2nd part was bold and the first part was not bold, then it works fine. So it has something to do with when the beginning of the text selection is bold.
Upvotes: 0
Views: 1085
Reputation: 205875
Is there an action that allows you to "unbold" text?
Create and apply a suitable SimpleAttributeSet
such as normal
, seen here.
Upvotes: 2
Reputation: 324207
here is some of my code that makes the text bold:
Don't know the context of how that code is used. But I would say a better way to do this is to just use the default Bold Action found in the StyledEditorKit
.
Read the Swing tutorial on Text Component Features for a simple example of an editor. The example uses a JTextPane, but the concepts for creating the menu items will be the same.
Upvotes: 2