1

I want to give a image link on my pc to a row on jtextpane. I give "text/html" ttype to jtextpane

 jTextPane1.setContentType("text/html");  

and I wrote this code for give image:

html text:

<img src=  file:/"+myimageplace+" alt=\"Click to Open Image\" width=\"30\" height=\"30\">

this is working for showing image.

But I want to give that image to go to image like this :

<a href=\"file:/"+myimageplace+">\"<img src=  file:/"+mytext+" alt=\"Click to Open Image\" width=\"30\" height=\"30\"></a>

But this isnt working?

How can I do that? Thanks.

CompEng
  • 7,161
  • 16
  • 68
  • 122
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 28 '13 at 09:59
  • Actually, this example did not need much of a SSCCE.... it was simply a matter of understanding better what the questioner wanted. – Menelaos Apr 28 '13 at 10:10
  • @Andrew Thompson Just noticed your the host of that site :) – Menelaos Apr 28 '13 at 10:13
  • @user2328779 If your problem is that the link doesn't work you should edit your question to: "How can I make image link work in JtextPane?" – Menelaos Apr 28 '13 at 10:14
  • @meewoK Wrong! Try feeding something like `My Documents/` as the value for `myimageplace`. – Andrew Thompson Apr 28 '13 at 10:15
  • @Andrew Thompson "this is working for showing image." . This is a SE project obviously. Also, if you read the question correctly you would have noticed the user is not complaining about not being able to display the image but is complaining about opening the link. Secondly, they didn't have the link event handler in place, so that was part of the problem - a part they need to fix first. Thirdly, and finally, most browsers support file:/ format (afaik), but in any case that's something that will be dealt with if there is a problem. So, your short emphatic "Wrong!" is Wrong! :) – Menelaos Apr 28 '13 at 10:21
  • @meewoK No, you missed the part where because the string was not delimited, a path of 'My Documents' would not be correctly parsed at all. (It is not valid HTML, so using a path with spaces complicates the parsing.) – Andrew Thompson Apr 28 '13 at 10:23
  • @user2328779 But as Andrew said, you should also check your myimageplace. I'd do a system.out.println of the location of the folder you want open and see if that works in the browser. Additionally, do you want to use a browser or do you want to open windows explorer (file explorer) ? If you want to open windows explorer: http://stackoverflow.com/questions/11174055/open-up-windows-explorer-in-java – Menelaos Apr 28 '13 at 10:25

1 Answers1

1

You need to have an event/link handler related to link clicks for this to work. Even though your rendering HTML, without a specific link handler to handle clicks it will not open the window.

I am quoting from here: Hyperlink in JEditorPane

Add the link handler

By default clicking the links won't do anything; you need a HyperlinkListener to deal with them:

editor.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
           // Do something with e.getURL() here
        }
    }
});

How you launch the browser to handle e.getURL() is up to you. One way if you're using Java 6 and a supported platform is to use the Desktop class:

if(Desktop.isDesktopSupported()) {
    Desktop.getDesktop().browse(e.getURL().toURI());
}
Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • so please tell me how can I do that – CompEng Apr 28 '13 at 10:10
  • Answer has been updated with information and link to further information. Also read the related link that includes additional information on what you need :) – Menelaos Apr 28 '13 at 10:11
  • this is working but I wonder about desktop is working on everywhere? – CompEng Apr 28 '13 at 10:25
  • I checked http://stackoverflow.com/questions/102325/not-supported-platforms-for-java-awt-desktop-getdesktop and it seems after java6 that desktop should is supported - but not on all platforms, so it also depends on the operating system. I'm further checking... that's another question entirely :) – Menelaos Apr 28 '13 at 10:34
  • I would suggest you can also use: http://www.mkyong.com/java/open-browser-in-java-windows-or-linux/ in the case that Desktop is not supported in order to have a more robust system. – Menelaos Apr 28 '13 at 10:37