简体   繁体   English

如何在“Java Swing”中获取鼠标悬停事件

[英]How to get Mouse hover event in `Java Swing`

I have a JPanel with multiple components in it - like a few JLabels , JTextBoxes , JComboBoxes , JCheckBoxes etc. 我有一个包含多个组件的JPanel - 比如一些JLabelsJTextBoxesJComboBoxesJCheckBoxes等。

I want to display a pop up help window if the user hovers over these components for say 3 secs. 如果用户将鼠标悬停在这些组件上3秒钟,我想显示一个弹出帮助窗口。

So far I added a MouseListener to one of my Components and it does display the required pop up and help. 到目前为止,我在我的一个组件中添加了一个MouseListener ,它确实显示了所需的弹出窗口和帮助。 However I can't achieve it after 3 sec delay. 但是在3秒延迟后我无法实现它。 As soon as the user moves the mouse to through that area of the component the pop up displays. 一旦用户将鼠标移动到组件的该区域,弹出窗口就会显示。 This is very annoying as the components are almost unusable. 这非常令人讨厌,因为组件几乎无法使用。 I have tried using MouseMotionListener and having the below code in mouseMoved(MouseEvent e) method. 我尝试过使用MouseMotionListener并在mouseMoved(MouseEvent e)方法中使用以下代码。 Gives the same effect. 产生同样的效果。

Any suggestion on how can I achieve the mouse hover effect - to display the pop up only after 3 sec delay? 有关如何实现鼠标悬停效果的任何建议 - 仅在延迟3秒后显示弹出窗口?

Sample Code:(Mouse Entered method) 示例代码:(鼠标输入方法)

private JTextField _textHost = new JTextField();

this._textHost().addMouseListener(this);

@Override
public void mouseEntered(MouseEvent e) {

    if(e.getSource() == this._textHost())
    {
        int reply = JOptionPane.showConfirmDialog(this, "Do you want to see the related help document?", "Show Help?", JOptionPane.YES_NO_OPTION);
        if(reply == JOptionPane.YES_OPTION)
        {
            //Opens a browser with appropriate link. 
            this.get_configPanel().get_GUIApp().openBrowser("http://google.com");
        }
    }

}

Use a Timer in mouseEntered() . mouseEntered()使用Timer Here's a working example: 这是一个有效的例子:

public class Test {

    private JFrame frame;


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test test = new Test();
                test.createUI();
            }
        });
    }

    private void createUI() {
        frame = new JFrame();
        JLabel label = new JLabel("Test");
        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent me) {
                startTimer();
            }
        });

        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    }

    private void startTimer() {
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        JOptionPane.showMessageDialog(frame, "Test");
                    }
                });
            }
        };

        Timer timer = new Timer(true);
        timer.schedule(task, 3000);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM