set tooltip text at a particular location

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 13:55:40

问题


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?


回答1:


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

}



回答2:


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



来源:https://stackoverflow.com/questions/11375250/set-tooltip-text-at-a-particular-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!