0

I've been trying to add a contextmenu to a marker, but I can't figure out how to get the contextmenu to appear.

marker.addEventListener("rightclick", new MapMouseEvent() {
    @Override
    public void onEvent(MouseEvent event) {
        contextMenu.show(
            marker, marker.getPosition().getLat(), 
            marker.getPosition().getLng()
        );
    }
});

I tried to cast marker as Node, but that didn't work, help?

NotZack
  • 518
  • 2
  • 9
  • 22

1 Answers1

0

To display a popup menu on JxMaps you have to do the next actions:

  1. Switch JxMaps to the LIGHTWEIGHT mode (). In the HEAVYWEIGHT mode, the popup menu can be displayed under the map.
  2. Add the code that shows the PopupMenu to "click" the event handler.

Please take a look at the following example:

JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Test"));

MapView mapView = new MapView(new MapViewOptions(MapComponentType.LIGHTWEIGHT));
mapView.setOnMapReadyHandler(new MapReadyHandler() {
    @Override
    public void onMapReady(MapStatus status) {
        final Map map = mapView.getMap();
        map.setCenter(new LatLng(35.91466, 10.312499));
        map.setZoom(2.0);
        map.addEventListener("rightclick", new MapEvent() {
            @Override
            public void onEvent() {
                java.awt.Point pos = MouseInfo.getPointerInfo().getLocation();
                SwingUtilities.convertPointFromScreen(pos, mapView);
                popup.show(mapView, pos.x, pos.y);
            }
        });
    }
});
  • hmm ... this looks like swing? wrong tag in the question or ..? – kleopatra Dec 17 '18 at 12:10
  • Yes, this is Swing. The matter is, that it seems like there is no JavaFX function which returns cursor coordinates. Possibly some workarounds exist, however they are not related to JxMaps. The sample on Swing was provided to you as an example of what should be realized on JavaFX. – Serhii Fedchenko Dec 18 '18 at 15:21
  • thanks for the clarification :) FYI: as of openjfx11, there's a public Robot class which can be used to retrieve the mouse coordinates. Even without, personally, I would prefer to use the coordinates from mouseInfo to show/position a fx ContextMenu to keep the mix of swing and fx as limited as possible .. didn't try, though, might not work ;) – kleopatra Dec 20 '18 at 12:43