hello everyone, i am using OSM in a swing application. Now what i needed is that when i clicked the mouse then it must perform Zoom in/out on the OSM. i could not able to perform the actionClick action for the mouse. How can i able to do it. If anyone have any idea please suggest me. Thanks in advance....

Dani AI

Generated

Practical, actionable ways to implement click-to-zoom for an OSM view embedded in a Swing app — two common cases are covered: using a Swing OSM viewer (JMapViewer) or a custom OpenGL renderer (JOGL/GLCanvas). 's pointer to OSM docs is useful background; below are concrete examples and pitfalls to check if mouse events seem not to fire.

Example — JMapViewer (common Swing OSM viewer)

map.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
            map.zoomIn();   // JMapViewer exposes zoom actions
        } else if (SwingUtilities.isRightMouseButton(e)) {
            map.zoomOut();
        }
    }
});

If you want the clicked point to remain under the cursor after zoom, convert the pixel coordinates to geographic coordinates (your viewer API usually provides a helper) and set the map center to that geo point before changing zoom.

Example — custom OpenGL renderer (GLCanvas / JOGL)

canvas.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            zoom *= 1.2f;    // update a shared zoom variable
        } else if (SwingUtilities.isRightMouseButton(e)) {
            zoom /= 1.2f;
        }
        canvas.display();    // request a redraw; your GLEventListener should read 'zoom'
    }
});

Implement the actual zoom in your GLEventListener.display() by adjusting the projection or camera using the shared zoom value.

Troubleshooting and tips

  • Make sure the listener is added to the actual map component (not a parent) and that no glass pane or overlay intercepts events.
  • Mouse listeners run on the Swing Event Dispatch Thread, so simple state updates are safe there; if your renderer has its own animator thread (JOGL Animator), keep the shared state properly synchronized or update via invokeLater if needed.
  • Use getClickCount() to distinguish single vs double clicks, and SwingUtilities.isLeft/RightMouseButton for button checks.

References: , JOGL (JogAmp), Java MouseListener tutorial

For — try the minimal listener on the actual map component first; if it never fires, post the component class you used and whether any overlays or input managers are present.

Recommended Answers

All 4 Replies

Thank you for your reply. I have gone through the link which you have provided. But still no any sign of success. I am still trying.... If you give more suggestion regarding this will highly be appreciated.

Thanks

sorry that isn't my cup of java, I'm only Java Fan, I'm so lazy, my endless lazyness didn't, don't and probably will not allow me check this API for detalis (your confort too)

Ok! thank you. I will try myself....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.