gumba
gumba

Reputation: 72

How to check if the mouse is pressed out of an JFrame

Right now I am using a MouseListener to see if the mouse is pressed but it doesn't register when you press outside of an JFrame I would really need it to so how do I check for mouse events outside of a JFrame?

Upvotes: 0

Views: 360

Answers (3)

giusy
giusy

Reputation: 375

To know the status of mouse outside the window you can use:

Point point = MouseInfo.getPointerInfo().getLocation();

Unfortunatly java.awt.event.MouseMotionListener give you information about mouse movement inside your window.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109815

Right now I am using a MouseListener to see if the mouse is pressed but it doesn't register when you press outside of an JFrame I would really need it to so how do I check for mouse events outside of a JFrame?

  • then JFrame lost Focus, you can test by using WindowFocusListener

  • Focus is asynchronous, then everything inside windowGainedFocus and windowLostFocus should be wrapped into invokeLater

Upvotes: 3

Roberto Anić Banić
Roberto Anić Banić

Reputation: 1421

Add a window listener

addWindowListener(new WindowListener() {

        @Override
        public void windowOpened(WindowEvent arg0) {


        }

        @Override
        public void windowIconified(WindowEvent arg0) {


        }

        @Override
        public void windowDeiconified(WindowEvent arg0) {


        }

        @Override
        public void windowDeactivated(WindowEvent arg0) {


        }

        @Override
        public void windowClosing(WindowEvent arg0) {

        }

        @Override
        public void windowClosed(WindowEvent arg0) {

        }

        @Override
        public void windowActivated(WindowEvent arg0) {


        }
    });

Try out all the methods (window...) and see which one works out best for you! :) I'm not telling you exactly what to do because to learn you cant just copy paste!

Upvotes: 0

Related Questions