Results 1 to 5 of 5
  1. #1
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default GetIcon from jTextPane

    Hi

    I have a JTextPane where there is text and icon.

    I want to get the Icon to store it in a variable. What method do I need to do it? Can't find it in the API.

    Thx for help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this JTextPane holding a JLabel by chance?

  3. #3
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

    No, why?

    But to add a jLabel, I would add it with the JTextPane.insertComponent(), right? I know I can put an image in a JLabel.

    Unfortunately, I don't know either how to access a component from my JTextPane.

  4. #4
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

    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!!!

  5. #5
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

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

    1. Create your Icon using a Style

    - 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


    2.Find the Style in which you put your Icon and get it 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.

    //Create the Icon and put it in the jTextPane

    Style style = jTextPane1.addStyle("Icon", null);
    StyleConstants.setIcon(style, new ImageIcon("ImageLink"));
    StyledDocument doc = jTextPane1.getStyledDocument();
    try{
    doc.insertString(0, "Texte inutil", style);
    }catch (Exception e){
    //rien faire
    }

    //Get it back and put it back in the second jTextPane

    DefaultStyledDocument dDoc = (DefaultStyledDocument)jTextPane1.getStyledDocumen t();
    Enumeration list = dDoc.getStyleNames();
    String styleName = (String)list.nextElement();
    Style style = dDoc.getStyle(styleName);
    Icon a = StyleConstants.getIcon(style);

    jFrame1.setVisible(true);
    jTextPane2.setText("");
    Style style2 = jTextPane2.addStyle("ICON2", null);
    StyleConstants.setIcon(style2, a);
    StyledDocument doc2 = jTextPane2.getStyledDocument();

    try{
    doc2.insertString(0, " ", style2);
    } catch (Exception e){
    //rien faire
    }


    If you know a better way, pls let me know.

    N.B. How do it put my code in a code box?

Similar Threads

  1. JTextPane with servlets
    By kinabaloo in forum Advanced Java
    Replies: 2
    Last Post: 07-11-2008, 07:32 AM
  2. JTextPane Styles Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:48 PM
  3. [SOLVED] hyperlinks in JTextPane
    By olbion in forum New To Java
    Replies: 4
    Last Post: 05-05-2008, 04:28 AM
  4. how to insert tables into JTextPane
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 10:11 AM
  5. Transparent JTextPane
    By Ada in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 10:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •