Unfortunately for me the second way is the good one if I want to get my Icon back.

Now, I must get it back.

There don't seems to be any method to get your Icon back directly... jTextPane has a method getText(). That method will

indeed return your Icon, the problem is it won't look like your Icon. If you use the method 1, the getText() will return " "

at the place of your Icon, if you use the method 2, the getText() will return str (the String you use in insertString).

The only thing you can do is get your Style back and from your Style get your Icon.

I try it with 2 different ways

1.
- I get my StyledDocument from my jTextPane
- I get the Style at a specific position with getLogicalStyle(int pos) in StyledDocument

Unfortunately, for a reason I still don't understand it always return me the "default" style. So this way don't work.

2.
- get a DefaultStyledDocument from jTextPane with the method getStyledDocument with a typecast (DefaultStyledDocument)
- get a Enumeration of all the name of your Style use in this Document with getStyleNames
- run through the Enumeration
- select the good name (if you give them good name, you can use string match up for example)
- get your style from the name with getStyle(String name) from DefaultStyledDocument
- get the Icon in this Style with StyleConstants.getIcon(Style s);

And it work!!!

11-26-2008, 04:26 AM

Gudradain

You want to put an Icon in a jTextPane with Text and want to get this Icon back?

- get a DefaultStyledDocument from jTextPane with the method getStyledDocument with a typecast (DefaultStyledDocument)
- get a Enumeration of all the name of your Style use in this Document with getStyleNames
- run through the Enumeration
- select the good name (if you give them good name, you can use string match up for example)
- get your style from the name with getStyle(String name) from DefaultStyledDocument
- get the Icon in this Style with StyleConstants.getIcon(Style s);

And it work!!!

Here is an example. I create 2 jTextPane. In the first one, I put the Icon, then I get it back and put it in the Second.