Get mouse position of a JFrame even when its child component is focused

ⅰ亾dé卋堺 提交于 2019-12-11 19:23:30

问题


I want to get the mouse position of my JFrame. But when mouse is on its child component (such as a table, button that is added to the JFrame) MouseMotion event is no more listening. How can I get the mouse position?


回答1:


Assuming the use-case in your comment is the real issue to solve, the answer is to implement the mouseExited such that it checks whether the mouse is still somewhere over the frame and hide it only if not.

Something like:

MouseListener closer = new MouseAdapter() {

    @Override
    public void mouseExited(MouseEvent e) {
        Rectangle r = new Rectangle(sideBar.getSize());
        if (!r.contains(e.getPoint())) {
            sideBar.setVisible(false);
        }
    }

};



回答2:


I was trying to make a Sidebar on my Swing application, where sidebar is an undecorated JFrame. I have set to dispose it when mouse exits. But when I move the mouse over a component added to the sidebar, it disappears. Idea may be dumb I am new to Java.

You could implement mouseExited as kleopatra suggests, but do it similar to this:

MouseListener closer = new MouseAdapter() {
    public void mouseExited(MouseEvent e) {
        // obtain source frame and see if mouse has left it
        Container cnt;
        if (e.getSource() instanceof JFrame) {
            // our frame, no conversion needed
            cnt = (Container) e.getSource();
        } else {
            // inside a descendant
            cnt = SwingUtilities.getAncestorOfClass(
                    JFrame.class, e.getComponent());
            // convert mouse event to make it appear
            // as if the frame generated it (I think :D)
            e = SwingUtilities.convertMouseEvent(
                    e.getComponent(), e, (Component) cnt);
        }            
        Rectangle r = new Rectangle(cnt.getSize());
        if (!r.contains(e.getPoint())) {
            cnt.setVisible(false);
            // or whatever
        }
    }
};

This is meant to be set for all descendant components of your sidebar and itself. It should check if your mouse is still inside your sidebar no matter over which of it's children/descendants the mouse is hovering.

You should also consider using an undecorated JDialog instead of a JFrame.

The reason why your sidebar is disappearing might be that you only added a mouse listener to it and not to any of it's children. It might be counter-intuitive, but when your mouse pointer enters a child/descendant of the sidebar, a mouseExited event is generated for the sidebar and then a mouseEntered event is generated for whichever child/descendant the mouse has entered. This is just how Swing mouse events are designed to work and there ain't much you can do about it.



来源:https://stackoverflow.com/questions/18373337/get-mouse-position-of-a-jframe-even-when-its-child-component-is-focused

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