0

I have a program that draws coordinates to a screen. I need to add functionality that displays a tooltip with its location when hovering over each point. However, I can't get it to work properly and I'm not sure why.

Coordinate class:

public class Coordinate extends Ellipse2D.Double {
    private Point point;
    private String name;
    private int radius;

    public Coordinate(Point point, String name, int radius) {
        super(point.getX(), point.getY(), radius, radius);
        this.point = point;
        this.name = name;
        this.radius = radius;
    }
}

Grid class:

protected void paintComponent(Graphics graphics) {
        initOrigin();
        super.paintComponent(graphics);

        graphics.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

        // draw Y axis
        graphics.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
        if (coordinates != null) {
            drawCoordinates(graphics);
        }
    }

    protected void drawCoordinates(Graphics graphics) {
        System.out.println(coordinates.toString());
        for (int i = 0; i < this.coordinates.size(); i++) {
            int radius = coordinates.get(i).getRadius();
            int x = (int) Math.round(coordinates.get(i).getPoint().x * X_SCALING) + originX;
            int y = (int) Math.round(coordinates.get(i).getPoint().y * -1 * Y_SCALING) + originY; // flip bit to get vertical coordinates in proper place
            System.out.println(x);
            System.out.println(y);
            graphics.fillOval(x - radius, y - radius, radius * 2, radius * 2);
            setToolTipText(coordinates.get(i).toString());

        }
    }

@Override
    public String getToolTipText(MouseEvent event) {
        for (Coordinate coordinate : coordinates) {
            if (coordinate.contains(event.getPoint())) {
                return coordinate.toString();
            }

        }
        return null;
    }

I'm setting the tooltip in the drawCoordinates() method and overriding the getToolTip() method as well but nothing is popping up. Obviously I'm missing something but I'm not sure where to start. Can anyone point me in the right direction?

  • Back in the day, I created an AreaManager. I think it would be worth your time to check out (https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/shape/) – ControlAltDel Sep 20 '22 at 13:27
  • It doesn't have "tooltip" capabilities, but if you look at Buttonize, you should start to get ideas about how you could create a JLabel in the popup layer in a JLayeredPane (Instead of a JPanel) – ControlAltDel Sep 20 '22 at 13:30
  • @ControlAltDel Do you perhaps know of any components that I can use instead of an ellipse that has built-in tooltip functionality? It doesn't have to be an ellipse specifically; I was looking at ImageIcon but I don't think that has tooltip functionality either. – mgwqiwdd Sep 20 '22 at 13:48
  • You definitely need to remove `setToolTipText` from your drawCoordinates method. Never modify a component’s state while painting it. Your `getToolTipText` implementation looks correct; perhaps there is an issue with the Coordinate.contains method, or perhaps `initOrigin()` is applying a transformation that is causing the coordinates’ visible locations to differ from the x and y in the data? – VGR Sep 20 '22 at 13:51
  • Setting the tool tip text in the drawCoordinates() method doesn't help. This will always set the text to the last coordinate painted. You need to: 1) set the tool tip text to an empty value when the component is created to register tooltip processing for the component. 2) Override the `getToolTipLocation()` method to the placement of the tool top vollows the mouse. 3) Check out: https://stackoverflow.com/questions/7138914/swing-how-to-create-a-custom-jtooltip-like-widget-that-moves-with-the-mouse/7139057#7139057 for a basic example to get you started. – camickr Sep 20 '22 at 14:01
  • *I can't get it to work properly* - define what that means when asking a question. We can't guess what behaviour you see. Tell us what happens and what you expect to happen. For example, to you ever see a tooltip? If so, does the text ever change? – camickr Sep 20 '22 at 14:06
  • @mgwqiwdd "... instead of an ellipse" There are plenty of components both in J2SE and on GitHub. The issue is that (J)Component is defined as being a rectangle (x,y,width,height) if you want non-rectangular components, you will need something like what I created – ControlAltDel Sep 20 '22 at 14:32

0 Answers0