Results 1 to 3 of 3
  1. #1
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Rep Power

    Default 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:
    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);
    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?

    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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power

    Default 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)

  3. #3
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Rep Power

    Default 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

  1. Repositioning components based on mouse position
    By robbie.26 in forum AWT / Swing
    Replies: 3
    Last Post: 06-21-2011, 06:55 AM
  2. popup without mouse event
    By cool in forum AWT / Swing
    Replies: 3
    Last Post: 11-04-2010, 12:57 PM
  3. Problem with determing mouse position!!!
    By in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 05:09 PM
  4. Replies: 0
    Last Post: 06-25-2008, 10:32 AM
  5. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-28-2007, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts