jsfa11
jsfa11

Reputation: 513

how to make an String description popup based on mouse position in java

The problem is simple. I want to create a box and then have a small string popup near the mouse indicating that the current mouse position is "Inside" or "Outside" of the box. Though the solution is not obvious to me. I do not want a large dialog component, just something as small as the text itself, and short lived.

Can someone point me toward a class that would do this?

Upvotes: 0

Views: 88

Answers (1)

rafalopez79
rafalopez79

Reputation: 2076

Try setting a Tooltip to the box component and another tooltip to the background.

For instance:

public class Test {

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                final JFrame jf = new JFrame();
                jf.setSize(800, 600);
                jf.setTitle("Test");
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jf.getContentPane().setLayout(new BorderLayout());
                final JPanel panel = new JPanel();
                panel.setBackground(Color.red);
                jf.getContentPane().add(panel, BorderLayout.CENTER);
                jf.setVisible(true);

                final JPanel box = new JPanel();
                box.setPreferredSize(new Dimension(100, 100));
                box.setBackground(Color.black);

                panel.add(box, BorderLayout.CENTER);
                box.setToolTipText("Inside");
                panel.setToolTipText("Outside");
            }
        });
    }
}

In this case, this is an example for setting tooltip near or outside a point drawn on a panel:

public class Test {

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                final JFrame jf = new JFrame();
                jf.setSize(800, 600);
                jf.setTitle("Test");
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jf.getContentPane().setLayout(new BorderLayout());
                final int px = 200;
                final int py = 200;
                final int delta  = 10;
                final JPanel panel = new JPanel(){
                    @Override
                    protected void paintComponent(final java.awt.Graphics g) {
                        super.paintComponent(g);
                        final Color c = g.getColor();
                        g.setColor(Color.yellow);
                        g.fillOval(px - delta/2, py - delta/2, delta, delta);
                        g.setColor(c);
                    }
                };
                panel.setBackground(Color.red);

                panel.addMouseMotionListener(new MouseAdapter() {
                    @Override
                    public void mouseMoved(final MouseEvent e) {
                        final int x = e.getX();
                        final int y = e.getY();
                        if (Math.abs(x - px) < delta && Math.abs(y - py) < delta){
                            panel.setToolTipText("Point!");
                        }else{
                            panel.setToolTipText("Outside");
                        }
                    }
                });

                jf.getContentPane().add(panel, BorderLayout.CENTER);
                jf.setVisible(true);
            }
        });
    }
}

Upvotes: 1

Related Questions