java - set tooltip text at a particular location -
i have output window shown here
complete code is: http://codes-at-igit.weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
circles different g.p.s locations. want show location i.e. , longitude , latitude when mouse hovers on node. tried set tool tip text doesn't give privilege specify locations @ text should occur. have coded in swing java . working in netbeans 7.1.2. how can this? how set tool tip text @ particular position?
you can override public string gettooltiptext(mouseevent event)
of underlying jcomponent. based on location of event can return null or tooltip related node.
here small snippet demonstrating this:
import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.beans.transient; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.tooltipmanager; public class testtooltip { private static class circlepanel extends jpanel { private ellipse2d circle1 = new ellipse2d.double(0, 0, 20, 20); private ellipse2d circle2 = new ellipse2d.double(300, 200, 20, 20); private ellipse2d circle3 = new ellipse2d.double(200, 100, 20, 20); public circlepanel() { // register component on tooltip manager // #gettooltiptext(mouseevent) gets invoked when mouse // hovers component tooltipmanager.sharedinstance().registercomponent(this); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); // simple paint of 3 circles on component g.setcolor(color.red); graphics2d g2 = (graphics2d) g; g2.fill(circle1); g2.fill(circle2); g2.fill(circle3); }; /** * method called automatically when mouse on component. * based on location of event, detect if on 1 of * circles. if so, display information relative circle * if mouse not on circle return tooltip of * component. */ @override public string gettooltiptext(mouseevent event) { point p = new point(event.getx(), event.gety()); string t = tooltipforcircle(p, circle1); if (t != null) { return t; } t = tooltipforcircle(p, circle2); if (t != null) { return t; } t = tooltipforcircle(p, circle3); if (t != null) { return t; } return super.gettooltiptext(event); } @override @transient public dimension getpreferredsize() { // size have return new dimension(350, 350); } protected string tooltipforcircle(point p, ellipse2d circle) { // test check if point inside circle if (circle.contains(p)) { // p inside circle, return information // relative circle. return "circle: (" + circle.getx() + " " + circle.gety() + ")"; } return null; } } protected void initui() { jframe frame = new jframe("test tooltip"); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new circlepanel(); frame.add(panel); frame.pack(); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new testtooltip().initui(); } }); } }
Comments
Post a Comment