java - Clickable thumbnail image in a tooltip -
i have special sort of tooltip appear when user hovers mouse cursor on specific item in treetable. tooltip thumbnail of pdf corresponds item in treetable cursor pointing at. in addition, i'd user able move cursor on thumbnail , click it, should open total pdf in system's default pdf reader (acrobat, adobe reader, etc.).
i realize tall order, i've done of work. i've discovered in huge programme need have settooltip()
method can retrieve appropriate thumbnail. in addition, since i've discovered having java create thumbnails pdf on fly far difficult, i've got things set there thumbnail jpgs pre-made. thus, settooltip()
command need somehow retrieve appropriate jpg. comes hard part.
at first, seemed easy. tried this convenient hack putting image in tooltip, , gets thumbnail showing properly. however, surrounding <img>
tag anchor tag (<a href="...">...</a>
) doesn't quite seem work. thumbnail surrounded tell-tale bluish border, alright, image remains un-clickable. in addition, tooltip disappears before it's image can clicked upon.
so thought might need more deep simple html hack. tried this more involved way of putting image in tooltip, seems work static image. need image different depending on what's beingness hovered on mouse cursor. in addition, how set method utilize 'custom version of tooltip' rather built-in one?
to give bit more context, location settooltip()
method seems work within of gettreecellrenderercomponent()
method, part of custom class extends jpanel
, implements treecellrenderer
. i'll post code if asked, might rather complicated , hard follow. thoughts?
edit 10/09/2014, 4:57pm: much of code may confusing, , that, apologize. suffice has putting tri-state checkbox within of jxtreetable. anyway, parts of import should easy plenty pick out, hope. can see, class extends jpanel
, cannot have extend jtooltip
well.
package info.chrismcgee.sky.treetable; import info.chrismcgee.beans.orderdetail; import info.chrismcgee.components.imagetooltip; import info.chrismcgee.components.tristatecheckbox; import info.chrismcgee.components.tristatestate; import info.chrismcgee.enums.ostype; import java.awt.borderlayout; import java.io.file; import javax.swing.jcomponent; import javax.swing.jpanel; import javax.swing.jtooltip; import javax.swing.jtree; import javax.swing.tree.treecellrenderer; import javax.swing.tree.treepath; import org.jdesktop.swingx.treetable.defaultmutabletreetablenode; public class skychecktreecellrenderer extends jpanel implements treecellrenderer { /** * */ private static final long serialversionuid = -2728513730497144120l; private skychecktreeselectionmodel selectionmodel; private treecellrenderer delegate; private boolean showrootnodecheckbox; private tristatecheckbox checkbox = new tristatecheckbox(""); protected skychecktreemanager.checkboxcustomizer checkboxcustomer; private string jobsfolderstr = ostype.getostype() == ostype.mac ? "/volumes/artdept/artdept/jobs" : "//skyfs/artdept/artdept/jobs"; public skychecktreecellrenderer(treecellrenderer delegate, skychecktreeselectionmodel selectionmodel, boolean showrootnodecheckbox) { this.delegate = delegate; this.selectionmodel = selectionmodel; this.showrootnodecheckbox = showrootnodecheckbox; setlayout(new borderlayout()); setopaque(false); checkbox.setopaque(false); } public jtooltip createtooltip() { homecoming new imagetooltip(); } private string gettooltiptext(defaultmutabletreetablenode node) { if (node.getuserobject() instanceof orderdetail) { orderdetail od = (orderdetail) node.getuserobject(); string thousandsfolderstr = jobsfolderstr + "/" + od.getorderid().substring(0, 3) + "000-" + od.getorderid().substring(0, 3) + "999/"; string productfolderstr = thousandsfolderstr + od.getorderid() + " folder/"; if (!od.getproductdetail().equals("")) productfolderstr = thousandsfolderstr + od.getorderid() + "/"; string img = productfolderstr + od.getorderid() + "_thumb.jpg"; if (!od.getproductdetail().equals("")) img = productfolderstr + od.getorderid() + "_" + od.getproductdetail() + "_thumb.jpg"; if (new file(img).exists()) homecoming "<html><img src=\"file://" + img + "\"></html>"; } homecoming null; } public jcomponent gettreecellrenderercomponent(jtree tree, object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasfocus) { jcomponent renderer = (jcomponent) delegate.gettreecellrenderercomponent(tree, value, selected, expanded, leaf, row, hasfocus); if (!showrootnodecheckbox && tree.getmodel().getroot() == value) { renderer.settooltiptext(gettooltiptext((defaultmutabletreetablenode)value)); homecoming renderer; } treepath path = tree.getpathforrow(row); if (path != null) { if (checkboxcustomer != null && !checkboxcustomer.showcheckbox(path)) { renderer.settooltiptext(gettooltiptext((defaultmutabletreetablenode)value)); homecoming renderer; } if (selectionmodel.ispathselected(path, selectionmodel.isdigged())) checkbox.gettristatemodel().setstate(tristatestate.selected); else checkbox.gettristatemodel().setstate(selectionmodel.isdigged() && selectionmodel.ispartiallyselected(path) ? tristatestate.indeterminate : tristatestate.deselected); } removeall(); add(checkbox, borderlayout.west); add(renderer, borderlayout.center); settooltiptext(gettooltiptext((defaultmutabletreetablenode)value)); homecoming this; } }
i need somehow extend jtooltip, , skychecktreecellrenderer class needs somehow reference custom tooltip. guess of getting involved , complex simple brain having problem wrapping around all. apologies.
how set method utilize 'custom version of tooltip' rather built-in one?
as illustration shows need extend component utilize custom tool tip.
i need image different depending on what's beingness hovered on mouse cursor
then need override gettooltiptext(mouseevent)
method homecoming text string represent image want display.
however, surrounding tag anchor tag (...) doesn't quite seem work
you need utilize jeditorpane if want respond hyperlink. read jeditorpane api example.
so suggest need utilize custom jtooltip, uses jeditorpane display appropriate image appropriate hyperlink. here illustration shows how utilize jlabel added component tool tip. should able modify code utilize jeditorpane.
also, need extend tree table utilize custom jtooltip.
import java.awt.*; import java.awt.image.bufferedimage; import java.net.url; import java.io.*; import javax.imageio.imageio; import javax.swing.*; public class tooltipimage extends jtooltip { private image image; public tooltipimage(image image) { this.image = image; setlayout( new borderlayout() ); add( new jlabel( new imageicon( image) ) ); } @override public dimension getpreferredsize() { homecoming new dimension(image.getwidth(this), image.getheight(this)); } private static void createandshowgui() throws exception { final bufferedimage testimage = imageio.read(new file("dukewavered.gif")); string[] columnnames = {"column 0", "column 1"}; object[][] info = { {"cell 0,0", "cell 0,1"}, {"cell 1,0", "cell 1,1"} }; jtable table = new jtable(data, columnnames) { public jtooltip createtooltip() { homecoming new tooltipimage( testimage ); } }; // set tool tip text table registered w/ tool tip manager table.settooltiptext(" "); jframe frame = new jframe("tool tip image"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add( new jscrollpane(table) ); frame.setlocationbyplatform( true ); frame.pack(); frame.setvisible( true ); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { seek { createandshowgui(); } catch(exception e) { system.out.println(e); } } }); } }
java image swing tooltip clickable
No comments:
Post a Comment