2

I am developing a small Swing app with tooltips on labels. This app has to react on mouse events such as click or exiting the frame.
With no tooltip, the app behaves correctly, but if I add tooltips I don't have mouse events anymore.

I wrote a small example to explain my problem, a JFrame with 3 Jlabels. The one in the center has a tooltip :

package testJtooltip;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;

public class TestToolTip {
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setSize(new Dimension(100,150));
        frame.setLocation(100, 100);
        frame.setUndecorated(true);
        frame.addMouseListener (
                new MouseListener () {
                    public void mouseClicked(MouseEvent e) {}
                    public void mouseEntered(MouseEvent e) {}
                    public void mouseExited(MouseEvent e) {
                        System.out.println("exited");
                    }
                    public void mousePressed(MouseEvent e) {
                        System.exit(0);
                    }
                    public void mouseReleased(MouseEvent arg0) {}
                }
                );


        Border border = BorderFactory.createLineBorder(Color.BLUE, 1);

        JLabel label1 = new JLabel();
        label1.setBorder(border);
        label1.setText("top");
        label1.setPreferredSize(new Dimension(100, 50));
        frame.add(label1,BorderLayout.PAGE_START);

        JLabel label2 = new JLabel();
        label2.setBorder(border);
        label2.setText("center");
        label2.setPreferredSize(new Dimension(100, 50));
        label2.setToolTipText("tooltip");
        frame.add(label2,BorderLayout.CENTER);

        JLabel label3 = new JLabel();
        label3.setBorder(border);
        label3.setText("bottom");
        label3.setPreferredSize(new Dimension(100, 50));
        frame.add(label3,BorderLayout.PAGE_END);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

So, my problems are :
1) click on the center label doesn't exit (while click on the others exits).
2) no mouseExited is fired when exiting the frame by the left or right at the level of the center label (while the event is fired exiting elsewhere).
3) a mouseExited event is fired when moving from the top or bottom label to the center.

It "seems" that the JLabel with the tooltip is "outside" the frame.

What did I miss ? How could I repair that, specially the mouseExited event ?

Carminou
  • 21
  • 2
  • I believe here's what you are looking for: [Tooltip stealing mouse events](https://stackoverflow.com/questions/5305462/tooltip-stealing-mouse-events?rq=1) – M. Prokhorov Jul 03 '17 at 11:01
  • Thanks for your response, but NO, even if I click before the tooltip appears or ou ot the tooltip (but inside center label) the app doesn't exit. More, this could not solve the mousExited event problem... – Carminou Jul 03 '17 at 12:59
  • Searched and found exact solution. Refer here: https://stackoverflow.com/questions/14931323/jlabel-tooltip-interferes-with-mouselistener – Aman Jul 05 '17 at 08:23

2 Answers2

0

Events are dispatched to the bottom level component that has a listener.

First, you add a general level listener to the frame.

But then you add a tooltip to the label. The tooltip works by adding a listener to the label. So the mouseEntered/Exited events are handled by the tooltip listener. The event is NOT passed to the listener added to the frame.

If you want additional processing, then you need to also add your custom MouseListener to the label.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • It solves partially the problem. I can handle the mousePressed at the JLabel level, it works. But about the mouseExited, it doesn't work: I now have mouseExited when leaving the JFrame (ok) but also when moving from any of the 3 JLabel to an other. And that is not OK... – Carminou Jul 03 '17 at 14:51
  • @Carminou, I don't fully understand your requirement so it is up to you to implement the logic based on your new understanding of how events are handled. Instead of handling mouseEntered/Exited at the label level, maybe you can create a "wrapper" panel. So you add the labels to the "wrapper" panel and handle mouseEntered/Exited on the "wrapper" panel. Maybe this answer will give you some ideas: https://stackoverflow.com/questions/42579137/how-to-avoid-child-gui-objects-to-take-over-mouse-listener/42584044#42584044 – camickr Jul 03 '17 at 15:10
  • in the reality, my app is much more complex and has a lot of JLabels with tooltip along one side of the enclosing window. When the cursor goes out of the window the display should go in a "standby" state (let's say grey). So I wrote the mouseExited handler at the outer fame level to reset the display to standard whenever the cursor leaves the window. But as shown above, when the cursor leaves the window through one of the JLabels that have a tooltip, the JFrame doesn't receive the mouseExited event when the cursor leaves the window. – Carminou Jul 04 '17 at 06:16
-1

Please update the section of your code with the below given code:

frame.addMouseListener(new MouseAdapter() {
    public void mouseExited(MouseEvent e) {
        System.out.println("exited");
    }

    public void mousePressed(MouseEvent e) {
        System.exit(0);
    }
});

Basically use MouseAdapter anonymous class instead of using MouseListener anonymous class. I used the same with your code and it is working fine.

Hope this will help. :-)

Aman
  • 735
  • 1
  • 6
  • 19
  • Sorry, I did the modification (MouseAdapted instead of MouseListener) but it doesn't solve anything. Still the wrong mouseExited events and no mousePressed event on the center button. – Carminou Jul 03 '17 at 13:02
  • Searched and found exact solution. Refer here: https://stackoverflow.com/questions/14931323/jlabel-tooltip-interferes-with-mouselistener – Aman Jul 05 '17 at 10:32