Skip to next Post if you only want constructive element...
Ok, so after 2 days reading the API, I find how to do it -_-.
Inserting an icon in a jTextPane is pretty easy. There is 2 ways you can do it.
1. use the method insertIcon(Icon c) in the jTextPane
2.
- create a style
- add it to the jTextPane
- use the StyleConstants.setIcon(Style s, Icon c) to define your style
- create a StyledDocument
- return a StyledDocument with the method getStyledDocument() dans jTextPane
- uuse the method insertString(int pos, String str, Style s) to insert your Icon
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!!!
阅读(2321) | 评论(0) | 转发(0) |