How to use tooltips on rectangle created by mouse movement - Java

Viewed 128

I am creating a rectangle with my mouse and i'm just trying to return info from the created object with a mouseover. I've been solving this with a messabox but there's just not many country to mouseover so the number of popup can be monstrous.

I decided to use a tooltip. Thing is it's not working on my mouse over :

       JTextField text = new JTextField();
    if (coordX > coordXRec && coordX < coordXRec + width && coordY > coordYrec && coordY < coordYrec + height){
        text.setToolTipText(i.GetInfoPays());
        text.getToolTipText();
}

Naturally, i want it to show when i mouseover my rectangle created :

That green rectangle was made by the user so i can't 'preset' an event or use it as a panel for my tooltip.

This is what excepted, but using a tooltiptext, not a messabox.

I've been using a MouseEventMoved to know if im hovering over my rectangle. Its working but i'm stuck at sort of changing my messagebox into a tooltip.

1 Answers

Use setToolTipText("..."); with the appropriate String when needed (e.g. during paintComponent()).

enter image description here

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;

public class MouseShapeDetection {

    private JComponent ui = null;

    MouseShapeDetection() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));
        
        ui.add(new ShapePanel());
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            MouseShapeDetection o = new MouseShapeDetection();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

class ShapePanel extends JPanel {

    Point point = new Point(0, 0);
    Dimension preferredSize = new Dimension(600, 300);
    ArrayList<Shape> shapes = new ArrayList<>();
    Color translucent = new Color(0,0,255,87);
    Color selectedColor = Color.GREEN;
    Color unselectedColor = Color.RED;

    public ShapePanel() {
        this.addMouseMotionListener(new MotionListener());
        setBackground(Color.WHITE);
        Random r = new Random();
        int x, y, w, h, wP = preferredSize.width, hP = preferredSize.height;
        for (int ii = 0; ii < 40; ii++) {
            w = r.nextInt(100)+40;
            h = r.nextInt(50)+20;
            x = r.nextInt(wP - w);
            y = r.nextInt(hP - h);
            Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, w, h);
            shapes.add(ellipse);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        StringBuilder sb = new StringBuilder("Shapes: ");
        Graphics2D g2 = (Graphics2D)g;
        for (int ii=0; ii<shapes.size(); ii++) {
            Shape shape = shapes.get(ii);
            g2.setColor(translucent);
            g2.fill(shape);
            if (shape.contains(point)) {
                g2.setColor(selectedColor);
                sb.append(ii);
                sb.append(" ");
            } else {
                g2.setColor(unselectedColor);
            }
            g2.setStroke(new BasicStroke(2.5f));
            g2.draw(shape);
        }
        setToolTipText(sb.toString());
    }

    @Override
    public Dimension getPreferredSize() {
        return preferredSize;
    }

    class MotionListener extends MouseMotionAdapter {

        @Override
        public void mouseMoved(MouseEvent e) {
            point = e.getPoint();
            repaint();
        }
    }
}
Related