Tooltip stealing mouse events

旧巷老猫 提交于 2019-12-11 05:59:49

问题


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.

 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:


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.


来源:https://stackoverflow.com/questions/5305462/tooltip-stealing-mouse-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!