0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
smith8ar
  • 91
  • 1
  • 9

2 Answers2

2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Is there an action that allows you to "unbold" text? – smith8ar Sep 18 '13 at 17:53
  • @AudreySmithDeyerle, Did you try the code from the tutorial? The Actions are designed to toggle the attributes. – camickr Sep 18 '13 at 20:57
  • I did try implementing the code from the tutorial. It works in all cases except for one. When I open the editor and type text and bold and unbold it, it works fine. The I tried saving the html that was output by the editor to a text file (via getText), then load it back into the editor via setText, I am unable to unbold any bold text. When I try to unbold the text, the editor reflects the change, but when I try to save the html, the tags are still there so it is still bold when it is loaded back in. Any idea why it works fine for text from scratch but not on text thats been loaded in? – smith8ar Sep 20 '13 at 14:57
  • I also have the same issue with trying to change the color of the text, once it's loaded back in, I can't change it. Could it be that the editor is not recognizing the style of the text when it's loaded in? – smith8ar Sep 20 '13 at 15:03
  • I don't play with JEditorPane and HTML. All I can say is if the text is exported and saved with proper tags then it should work. I don't know why extra tags might be left in the HTML. Make sure you are using the write(...) method of the JEditorPane. – camickr Sep 20 '13 at 15:16
2

Is there an action that allows you to "unbold" text?

Create and apply a suitable SimpleAttributeSet such as normal, seen here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045