问题
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