Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set tooltip text at a particular location

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?

like image 951
user1503567 Avatar asked Jul 07 '12 13:07

user1503567


People also ask

How do I set tooltip location?

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.

How do I set dynamic position in tooltip?

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.

How do I create a custom tooltip?

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.

Which method is used to set the 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.


2 Answers

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();
            }
        });
    }

}
like image 179
Guillaume Polet Avatar answered Sep 22 '22 10:09

Guillaume Polet


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

like image 20
murkle Avatar answered Sep 23 '22 10:09

murkle