Trying to get 'frame.getContentPane().getCursor().getType();' to read outside frame
The program below outputs mouse coordinates anywhere on the screen every 500 msec and tries to get the cursor type.
The problem is cursor type always shows 0.
Sample output:
PHP Code:
C:Program FilesJavajdk1.6.0_23bin>java cursorType
hello
(1058, 837)
Cursor Type is 0
(1058, 837)
Cursor Type is 0
(1103, 818)
Cursor Type is 0
(1104, 816)
Cursor Type is 0
(1107, 867)
PHP Code:
import java.sql.Timestamp;
import java.util.Date;
import java.awt.Robot;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.SystemTray;
import java.util.Random;
import java.awt.Cursor;
class cursorType {
public static void main(String args[]) throws InterruptedException {
System.out.println("hello");
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create Label...
Label lbl = new Label("Welcome!", Label.CENTER);
frame.getContentPane().add(lbl, BorderLayout.CENTER);
//4. Size the frame.
frame.setSize(1919, 999);
//frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//5. Show it.
frame.setVisible(true);
int cursorType;
while (true) {
Thread.sleep(500);
System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + ", " + MouseInfo.getPointerInfo().getLocation().y + ")");
cursorType = frame.getContentPane().getCursor().getType();
System.out.println("Cursor Type is " + cursorType);
}
}
}
*used php tags cause I like its color coding
I understand that .getCursor().getType(); has to work under the confines of a frame but is there a way to workaround this?
Bottom line is I want to capture the current cursor type anywhere on my screen.
Any ideas?
Re: Trying to get 'frame.getContentPane().getCursor().getType();' to read outside fra
.getCursor().getType()
doesn't tell what the current cursor is, even if the cursor is over the frame. That method only returns what the mouse would be if it was over that JFrame, JComponent, etc.
So the best thing I can suggest is finding which Component the mouse is over (or if it's over the JFrame at all), and then return the cursor for whatever component the mouse is hovering over.