Search code examples
javaswingjwindow

How do I make my JWindow window always stay focused


I am making a java application that includes a JWindow. I want to be able to track the mouse without the user having to click the window after going to another window.


Solution

  • Your question is little vague on why you want to continue processing the mouse once it's left the JWindow...but

    You have two (basic) choices when it comes to mointoring the mouse outside of your application, you can use a JNI/JNA solution or you can poll MouseInfo.

    The following demonstrates the latter, using MouseInfo and a javax.swing.Timer to update a label...

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.MouseInfo;
    import java.awt.PointerInfo;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class MouseWindow {
    
        public static void main(String[] args) {
            new MouseWindow();
        }
    
        public MouseWindow() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JLabel label;
    
            public TestPane() {
                setLayout(new BorderLayout());
                label = new JLabel();
                label.setFont(label.getFont().deriveFont(48f));
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setVerticalAlignment(JLabel.CENTER);
                add(label);
                updateMouseInfo();
    
                Timer timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        updateMouseInfo();
                    }
                });
                timer.start();
            }
    
            protected void updateMouseInfo() {
                PointerInfo pi = MouseInfo.getPointerInfo();
                label.setText(pi.getLocation().x + "x" + pi.getLocation().y);
            }            
        }
    }
    

    Updated

    You may also find Window#setAlwaysOnTop of help to keep the window ontop of the others, if support for the platform