Results 1 to 3 of 3
- 02-29-2012, 05:32 PM #1
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Mouse position - rootpane/frame vs popup?
I'm currently using the following code to calculate the mouse position in a JPanel, on a JPanel, embedded in a JFrame:
I'm not using a simple this.getMousePosition() because the panel can't always access the mouse. The code above is designed to get around that through some basic vector calc, but it hits a snag (npe for p because m returns null) whenever the mouse passes over a popup menu. I can only presume that the popup blocks even the frame from getting access to the mouse. Any ideas on this one?Java Code:Point r= getRootPane().getLocationOnScreen(); //Top left of JFrame, relative to screen Point m=getRootPane().getMousePosition(); //Mouse relative to top left of JFrame Point l=getParent().getLocationOnScreen(); //Top left of middle JPanel, relative to screen Point p=new Point(m.x-l.x+r.x, m.y-l.y+r.y);
SSCCE: Honestly, got a hundred lines in and can't reproduce the error. I guess this'll be a general knowledge request for interactions between RootPane and popups.
Edit: at the moment, I'm looking into rolling the aforementioned JPanels into each other, since they're fairly close to one another in terms of interaction. I separated them so that the "outer" one could act as a glass pane. Unfortunately, glass pane seems to fail when it comes to popups.Last edited by Diargg; 02-29-2012 at 05:38 PM.
- 02-29-2012, 06:26 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Mouse position - rootpane/frame vs popup?
Why not just use a MouseListener to track mouse events within the panel itself?
How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
- 02-29-2012, 06:31 PM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Mouse position - rootpane/frame vs popup?
I have a mouse listener as well, doing other things, but the issue is that once the mouse is no visible to the JPanel, mouse listeners cut out as well. In the program, there are smaller JPanels that "float" above the glasspane, blocking the view.
I did discover more odd behavior though. The issue is not with the mouse per se, but with the popup menu. If you right click anywhere such that the popup menu would be drawn partially outside of the frame, it npe's. However, dragging the mouse over to the edge and spawning a popup menu (right click and hold, drag to edge, release) works fine, and prevents the issue form occurring later on.
Very lost here.
Similar Threads
-
Repositioning components based on mouse position
By robbie.26 in forum AWT / SwingReplies: 3Last Post: 06-21-2011, 06:55 AM -
popup without mouse event
By cool in forum AWT / SwingReplies: 3Last Post: 11-04-2010, 12:57 PM -
Problem with determing mouse position!!!
By G.rocks in forum New To JavaReplies: 6Last Post: 07-15-2010, 05:09 PM -
How to get mouse hit position in a textlayout using TextHitInfo class
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:32 AM -
Use the mouse position
By susan in forum Java AppletsReplies: 1Last Post: 07-28-2007, 11:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks