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.
Asked
Active
Viewed 990 times
0
-
Not sure what that has to do with keeping the window focused, because you'd never be able to get any work done. If you want to do "global" mouse monitoring, you can either implement a JNI solution or write a `Thread` that polls the [`MouseInfo`](http://docs.oracle.com/javase/7/docs/api/java/awt/MouseInfo.html) for information about the `PointerInfo`. Remember though, the location of the mouse that this returns in screen coordinates ;) – MadProgrammer Sep 19 '13 at 02:18
1 Answers
2
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...
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

MadProgrammer
- 343,457
- 22
- 230
- 366
-
Q: JWindow isn't modal A: and if yes then OP will need direction to undecorated JDialog with some of Modality_Types and always_on, – mKorbel Sep 19 '13 at 05:39
-
@mKorbel If the OP cares to share some more context/details, I'd be happy to elaborate, but your are right. AlwaysOnTop would be useful... – MadProgrammer Sep 19 '13 at 05:40