10

Java Swing question.

I have a JPanel which displays a graph. When I move the mouse over this graph, I want certain information to be displayed on a tooltip-like widget that moves with the mouse. How can I best implement this?

I guess my problem will be solved if I know how to position a custom JComponent absolutely within the JPanel that acts as my drawing canvas. I could then trap the mouse moved event and reposition/update the widget. Any other solution (including possibly using JToolTip directly) would also be very much welcome!

hoijui
  • 3,615
  • 2
  • 33
  • 41
ARV
  • 6,287
  • 11
  • 31
  • 41
  • that isn't easy job, all examples that I saw were too long and based on JComponent's Model + SwingUtilities + JWindow, maybe someone have easies idea – mKorbel Aug 21 '11 at 15:13

1 Answers1

11

Override the getToolTipText(MouseEvent) method to dynamically set the tool tip based on the mouse location.

Edit:

If you want the tooltip to continually move with the mouse then you will also need to override the getToolTipLocation() method.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ToolTipPanel extends JPanel
{
    public ToolTipPanel()
    {
        setPreferredSize( new Dimension(200, 200) );
        setToolTipText("");
    }

    public void paintComponent(Graphics g)
    {
        g.setColor( Color.red );
        g.fillRect(0, 0, 100, 200);
        g.setColor( Color.blue );
        g.fillRect(100, 0, 100, 200);
    }

    public String getToolTipText(MouseEvent e)
    {
        if (e.getX() < 100)
            return "red";
        else
            return "blue";
    }

    public Point getToolTipLocation(MouseEvent e)
    {
        Point p = e.getPoint();
        p.y += 15;
        return p;
//      return super.getToolTipLocation(e);
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( new ToolTipPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • maybe but I never saw tooltip flying/follows mouse on the screen and with connection to the JComponent under the mouse Cursor +1 – mKorbel Aug 21 '11 at 15:17
  • thanks very nice example, then which of ways would be better option 1) to check JComponent by using SwingUtilities, then change tooltip properties from some pre-prepared Array 2) set tooltip only for expected JComponent directly and in this case JComponet returns properties back to the tooltip – mKorbel Aug 21 '11 at 15:48
  • 1
    If you are adding components to the panel then you would add a tooltip to each component. If you are painting images then you would need to track the shape of each image and iterate through all the shapes to determine where the mouse point is and then set the appropriate tool tip for the image. – camickr Aug 21 '11 at 16:11
  • Many thanks. I want the user to be able to set the tooltip to be visible or not. Is there a way I can control when the tooltip will be displayed? – ARV Aug 21 '11 at 16:31