Java tooltip with image in JAR file

强颜欢笑 提交于 2019-12-01 11:10:29

问题


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


回答1:


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"));
}



来源:https://stackoverflow.com/questions/16154792/java-tooltip-with-image-in-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!