I have my output window as shown here
My complete code is:
http://codes-at-igit.weebly.com/uploads/1/2/2/7/12272842/travellingsalesmanproblem.java
The circles are different G.P.S locations. I want to show the location i.e. , the longitude and latitude when mouse hovers on a node. I tried set tool tip text but it doesn't give privilege to specify the locations at which the text should occur. I have coded it in swing Java . I am working in Netbeans 7.1.2. So how can I do this?
How do I set tool tip text at a particular position?
You can position a ToolTip by setting the PlacementTarget, PlacementRectangle, Placement, HorizontalOffset, and VerticalOffsetProperty properties. These properties behave the same as they do for a Popup.
The Tooltip and its tip pointer can be positioned dynamically based on the target's location. This can be achieved by using the refresh method, which auto adjusts the Tooltip over the target.
To make a simple tooltip, we'll first create the HTML element that triggers the tooltip when hovered over. We'll create this element as a div and assign it the class hover-text. Next, we'll create the element for the tooltip itself. This will be a span element with the class tooltip-text.
We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.
You can simply override public String getToolTipText(MouseEvent event)
of the underlying JComponent. Then based on the location of the event you can return null or the tooltip related to the node.
Here is a 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 the component on the tooltip manager
// So that #getToolTipText(MouseEvent) gets invoked when the mouse
// hovers the component
ToolTipManager.sharedInstance().registerComponent(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Simple paint of 3 circles on the component
g.setColor(Color.RED);
Graphics2D g2 = (Graphics2D) g;
g2.fill(circle1);
g2.fill(circle2);
g2.fill(circle3);
};
/**
* This method is called automatically when the mouse is over the component.
* Based on the location of the event, we detect if we are over one of
* the circles. If so, we display some information relative to that circle
* If the mouse is not over any circle we return the tooltip of the
* 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() {
// Some size we would like to have
return new Dimension(350, 350);
}
protected String tooltipForCircle(Point p, Ellipse2D circle) {
// Test to check if the point is inside circle
if (circle.contains(p)) {
// p is inside the circle, we return some information
// relative to that 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();
}
});
}
}
Try over-riding getToolTipLocation(), for example:
JButton buttonAbove = new JButton("Above") {
public Point getToolTipLocation(MouseEvent e) {
return new Point(20, -30);
}
};
from here: http://www.java2s.com/Code/Java/Swing-JFC/ToolTipLocationExample.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With