Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hovering: setLocation does not work (wrong position) #27

Closed
ghost opened this issue May 26, 2014 · 7 comments
Closed

Hovering: setLocation does not work (wrong position) #27

ghost opened this issue May 26, 2014 · 7 comments

Comments

@ghost
Copy link

ghost commented May 26, 2014

Hi,
I'm performing this tutorial: https://today.java.net/pub/a/today/2007/11/13/mapping-mashups-with-jxmapviewer.html#waypoint-rollovers
The rollover JLabel is not added at the correct position, it is always displayed at the top, horizontal center.
screenshot from 2014-05-26 14 18 25

My code, however, should place it at the mouse pointer position:

  if (converted_gp_pt.distance(e.getPoint()) < 20) {
                            //System.out.println("IMPORTANT " + converted_gp_pt);
                            mapViewer.add(tooltip);

                            tooltip.setText(twitterDataManager.getContent(dataItem));
                            tooltip.setVisible(true);
                            System.out.println("set location " + e.getPoint());
                            tooltip.setLocation(e.getPoint());
                            break;
                        } else {
                            tooltip.setVisible(false);
                            mapViewer.remove(tooltip);
                        }
@ghost ghost changed the title Hovering JLabel does not work at correct position Hovering: setLocation does not work (wrong position) May 27, 2014
@msteiger
Copy link
Owner

msteiger commented Jun 2, 2014

Thanks - I will look at it asap ..

@ghost
Copy link
Author

ghost commented Jun 2, 2014

just wanted to let you know. I've also tried to add other Swing components, e.g., JPanel: the bug (?) is always the same, position is always top center.

@msteiger
Copy link
Owner

msteiger commented Jun 3, 2014

The original example from java.net are heavily outdated - still, the code should work. I was not able to reproduce the problem.

This is why I've created a new sample that uses JXMapKit and a roll-over tooltip.

Does this solve your problem?

@ghost
Copy link
Author

ghost commented Jun 17, 2014

Okay I think I've found the issue. I was iterating over a bunch of items and checked whether there was one item within a certain distance to the mouse pointer. If so, I set its position and breaked the for-loop. If not, I was calling tooltip.setVisible(false) and continued to check for the next item. Calling all these setVisible(false) before the first (and final) setVisible(true) made the functionality somehow broke. Here the broken code:

for (DataItem di : twitterDataManager.getDataItems()) {
    GeoLocation gl = twitterDataManager.getGeoLocation(di);
    ...

    // check if near the mouse
    if (screenPos.distance(e.getPoint()) < 20) {
        mytip.setLocation(screenPos);
        isVisible = true;
        break;
    } else {
        mytip.setVisible(false);
    }
}

And this is the version that works:

boolean isVisible = false;

// iter over all tweet locations
for (DataItem di : twitterDataManager.getDataItems()) {
    GeoLocation gl = twitterDataManager.getGeoLocation(di);
    ...

    // check if near the mouse
    if (screenPos.distance(e.getPoint()) < 20) {
        mytip.setLocation(screenPos);
        isVisible = true;
        break;
    } else {
    }
}
mytip.setVisible(isVisible);

So this still might be a bug, which is however caused by calling repeatedly setVisible(false) before actually calling setVisible(true) and setting the proper position.

@ghost
Copy link
Author

ghost commented Jun 17, 2014

However, I am experiencing flickering now. It seems like the label initially pops up at the top center position for a short time, before actually being displayed at the correct position.
This is an excerpt of the important lines of code, I think the flickering is not caused by my code but rather by some odd Swing / JXMapViewer behaviour?

if (closestDataItem != null) {
    tooltip.setDataItem(closestDataItem);
    tooltip.setLocation(closestScreenPos);
    tooltip.setVisible(true);
} else {
    tooltip.setVisible(false);
}

Here a video showing the flickering effect.
https://www.youtube.com/watch?v=XheubIIH-L8

@msteiger
Copy link
Owner

I can confirm this, but I cannot explain the jumping behaviour of the tooltip. Please let me know if you find the reason for that.

@ghost
Copy link
Author

ghost commented Jun 24, 2014

Alright, if I find something, I'll let you know here :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant