Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2008, 09:53 AM
Member
 
Join Date: Nov 2008
Posts: 38
Gudradain is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-23-2008, 09:59 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
Is this JTextPane holding a JLabel by chance?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-23-2008, 10:30 AM
Member
 
Join Date: Nov 2008
Posts: 38
Gudradain is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2008, 05:16 AM
Member
 
Join Date: Nov 2008
Posts: 38
Gudradain is on a distinguished road
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!!!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2008, 05:26 AM
Member
 
Join Date: Nov 2008
Posts: 38
Gudradain is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 12:40 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org