Sharcoux
Sharcoux

Reputation: 6085

edit html content in JEditorPane

I really need your help as I'm trying for one month now to do something using various approaches... unsuccessfully.

I have a JTextPane containing a html text with images, etc. This works perfectly.

Now, I have the id of a < span > < /span > element and I want to delete it's content and insert a component instead.

More concretely, I have : < span id='123' >< img src='url/123.png' / >< /span > and I want to have < span id='123' >#JLabel< /span >

I know how to :

I have no idea how I can :

Thank you for your help !

Upvotes: 0

Views: 1103

Answers (3)

camickr
camickr

Reputation: 324118

You know how to get the Span Element so maybe the following will work:

  1. The Element class has a getStartOffset() method which you might be able to use to insert the JLabel.

  2. Take a look at HTMLDocument class. There is a remove(Element) method.

Upvotes: 1

desperateCoder
desperateCoder

Reputation: 700

why are you adding a jlabel to a TextPane?

anyway, since HTML is a XML-dialect, you could use a XML-parser to organize your tags and change contents and attributes.

so you could update the content of your Text-Pane by simply calling setText()

Upvotes: 0

Daniel Pereira
Daniel Pereira

Reputation: 2785

You can use a regex to do it. Here's an example of one that makes what you want, but does not handle whitespaces:

<img src='.+?'(/>|></img>)

In your example, if you ignore whitespaces, it would be like this:

String html = "< span id='123' ><img src='url/123.png'/>< /span >";
String newValue = html.replaceFirst("<img src='.+?'(/>|></img>)", "myJLabel");

To make it work with whitespaces, just add \s* wherever they can appear. Here's an example that allow both <img and < img

<\\s*img src='.+?'(/>|></img>)

Now, to make it work as you need, just put the others \s* where you think it's necessary

To learn more about regex, read this

Upvotes: 0

Related Questions