Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Frits Walraven
  • Himai Minh

Getting a JLabel to follow the cursor

Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a bit of a problem regarding the mouse. I am using MouseInfo.getPointerInfo().getLocation() to get the mouse pointer's location on the screen and trying to get a JLabel follow the mouse pointer.

When I do it on a new Project with a simple JFrame and the said JLabel, it works as it should. However, when I placed it on my real project, I encountered problems positioning the JLabel. The JLabel moves as the mouse moves but the location is wrong. The attached image explains how I set up my JFrame. The green area is the JFrame that contains all the components of my project. Inside it is the blue JPanel that holds other components. The re box is the JLabel I need to move. The purple are is another JPanel that holds yet other components. The orange ones are a custom JPanel.

I've set it up so that when the mouse enters the orange area, the red JLabel moves towards the cursor's position. Then when the cursor moves while inside the orange JPanel, the JLabel follows along. When the cursor leaves the orange JPanel, it resets back to its position. The problem is, the MouseListener is inside the orange JPanel and I'm controlling the red JLabel from there. When I tested it out, the position of the JLabel is wrong, or rather, is way off the desired position (as MouseInfo.getPointerInfo().getLocation() gets the position of the cursor on the screen).

How should I go about this problem? One solution I can think of is to remove the purple JPanel and simply put the orange ones on the blue JPanel. However, the orange JPanels are placed there on a loop with a set of conditions and parameters, meaning, the orange JPanels are dynamic and can be there or not. Any other possible solutions besides this one?

EDIT: I realized this was placed on a wrong forum. Sorry about that.
[Thumbnail for sample.png]
How my JFrame is set up.
Posts: 3244
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The MousePointer location is relative to the screen. You need the location of the label relative to its parent. Check out the SwingUtilities class. It has method that do point conversion for you.
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic