Tooltip stealing mouse events

2019-07-31 10:51发布

When I have buttons near the button of the screen, the tooltip appears underneath the mouse. Clicking will then make the tooltip disappear, instead of clicking the button.

Tooltip Example

 public static void main(String[] args) {
    JFrame frame = new JFrame("Test");
    JButton button = new JButton("Test");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("action performed");
        }
    });
    button.setToolTipText("Sample tooltip text");
    frame.add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.pack();
}

Any idea how to ensure that the button receives the click in this case?

1条回答
可以哭但决不认输i
2楼-- · 2019-07-31 11:36

This will only happen if you stop your mouse on the button, wait for a tooltip, then move your mouse and click on the tooltip. If you click on the button before the tooltip appears or if you don't move the mouse to the tooltip before you click you/your users should be fine.

I believe this is exactly how a tooltip should work, you click on it to dismiss it. If this is causing issues, I would suggest one of three options:

  1. Set the delay for tooltips to be longer: ToolTipManager.sharedInstance().setInitialDelay()
  2. Don't show a tooltip at all
  3. Write your own mouse motion listener than displays a tool tip off to the side or in another part of the GUI.
查看更多
登录 后发表回答