Adding an image to a JTooltip without knowing the image's path (due to the image being a temporary ressource)
To be a little more specific: I've built a simple multithreaded TCPServer (Java) which communicates with clients (C# / mono). On request the Clients take a screenshot and send it to the Server. The screenshots are then displayed in a JTable. I'd like to add a tooltip to these images in the table with the same image but in another size. The code currently looks like this:
screenLabel.setToolTipText("<html><img src=\"" + (new ImageIcon(image)) + "\"></img> Tooltip</html>");
Of course, this won't work, as the src attribute requires a path. The problem is, I don't know how to circumvent this behaviour.
My question would hence either be: 1) How to retrieve a valid path for a temporary resource (without saving the resource in the file-system) OR 2) How to provid开发者_如何学Ce a custom JTooltip which easily allows Images/ImageIcons to be displayedIt is possible to add a new protocol besides the standard ftp/http/etc to the URL resolving which then maps into your own code instead of over the network.
There is three approaches listed at http://www.unicon.net/node/776, which may help you getting started. I was thinking of extending URLStreamHandler and registering it with the JVM.
Regarding changing JToolTip, from http://www.manning-sandbox.com/message.jspa?messageID=9915
"Remember JToolTip is a container, so , subclass JToolTip to have a JTextArea/Pane fill it . Set the JTA/JTP as uneditable and voila you have a multiline tooltip! Similarly you can display any custom component you want! "
For option 2 you need to:
a) create a custom tooltip
b) override the table to use the tooltip.
This posting provides a simple working example.
精彩评论