-1

I am making a text editor roughly like a microsoft word and I am using a textpane with HTML. After doing some research I found a way to apply html to a highlighted section on using StyleConstants and then a method that goes JTextPane.getStyledDocument().setCharacterAttributes();. Problem is that when I use the getText() method from the textpane, the styled text is missing. For example I input a normal string into the textpane, the getText() returns the html code and the text. When I style the text and use getText() all the styled words are deleted and normal words are preserved.

What I'm trying to accomplish is, to save the entire plain text (including the html) on a text file and when I load it back up and set it on the JTextPane, it keeps its style, but for some reason styled text is deleted on the "getText()" method and normal text is preserved normally. Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
rortega92
  • 19
  • 1
  • 1
    *"I am making a text editor roughly like a microsoft word and I am using a textpane with HTML."* The mixture of components make me think 'no you're not', unless your definition of 'roughly' is **very** wide. – Andrew Thompson Nov 27 '12 at 21:52
  • 2
    I agree with Andrew - the exercise seem relatively futile. If you're doing it just for fun, have at it. If you're doing this for anything that is intended to make a profit or be used by a lot of people, please stop now, and use an existing product. A decent editor would take many hundreds, possibly thousands of person-hours. Also, Swing is horrible way to try to do this, unless you absolutely have to. – GreyBeardedGeek Nov 27 '12 at 22:32
  • See also `HTMLDocumentEditor`, _et al_, cited [here](http://stackoverflow.com/a/3158708/230513). – trashgod Nov 28 '12 at 02:32
  • @AndrewThompson greybeardedgeek i said roughly cause its a class project. OF COURSE it isn't with the intention of selling it. Common sense people. – rortega92 Nov 28 '12 at 16:38
  • *"Common sense people."* ..would typically make that clear at the start, rather than having to explicitly state it as a comment. – Andrew Thompson Nov 28 '12 at 17:11

1 Answers1

3

You can implement your own custom Reader/Writer as described here http://java-sl.com/editor_kit_tutorial_reader_writer.html

When you use setCharacterAttributes the content stored in leaves Elements attributes so you can access them and store as you wish.

StanislavL
  • 56,971
  • 9
  • 68
  • 98