1

I'm using JFreeChart where I customized the chart's tooltip by implementing the XYToolTipGenerator interface. As the generateToolTip() method is supposed to return a String, here is what I did to show images in the tooltip:

@Override
public String generateToolTip(XYDataset dataset, int series, int item) {
    (...)
    return "<html><body><img src=\"file:resources/img/image.png\"></body></html>";
}

While this works perfectly when executing directly from my IDE (Eclipse), it obviously fails when being executed from a packaged JAR file (the image.png is also in the JAR file).

Any hint on how to solve this would be greatly appreciated (ideally without having to extract the image.png from the JAR file).

Many thanks, Thomas

Tom
  • 1,375
  • 3
  • 24
  • 45

1 Answers1

3

Try using resource URL:

URL url = getClass().getResource("/img/image.png");
String tt = "<html><body><img src='" + url + "'></body></html>";

Edit: simple example run from executable jar that shows tooltip:

@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
    return String.format(
            "<html><body><img src='%s'> some data </body></html>",
            getClass().getResource("/images/duke.gif"));
}

enter image description here

tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Unfortunately this doesn't work (image not found when showing tooltip). As the tooltip is shown by the OS (Windows), my guess is that I will have to provide the pictures outside the JAR file... unless someone has a brilliant workaround? Thx – Tom Apr 22 '13 at 19:59
  • @Tom it should work. Make sure the path in `getResource()` is correct. Where are the images located? See [Loading Images Using getResource](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource) tutorial. – tenorsax Apr 22 '13 at 20:02
  • @Tom I may have omitted `resources` folder in my example. Just adjust the path as needed. – tenorsax Apr 22 '13 at 20:14
  • Thx for the help but after a double checking everything (classpath, etc.): it doesn't work. Using other images directly with getResource() works, so it is definitely related to the tooltip. – Tom Apr 22 '13 at 20:23
  • @Tom try calling `JOptionPane.showMessageDialog(null, tt);` just to check that tooltip's HTML string is correct and you can see the image in a popup dialog. Also, if `generateToolTip` worked in IDE there is no reason why it should fail when executing from jar. Swing's HTML renderer should be able to process this. – tenorsax Apr 22 '13 at 20:28
  • It works like a charm! Mea culpa: I didn't put the lead "/" (i.e. I has written "img/image.png") and for some reason he requires "/img/image.png"). Thanks a lot for the help! – Tom Apr 23 '13 at 12:00
  • @Tom You're welcome! :) Note that [Class.getResource](http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29) is relative to the package containing the class unless a leading slash is specified. – tenorsax Apr 24 '13 at 03:42
  • Are you happy with this solution? In my case it seems to produce memory leaks if called often. – Stephan Jan 29 '14 at 16:48