Working with JMapViewer, a strange behavior of the component was recognized. I am using DefaultMapController to get the map position (lat, lon).
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
public class Test extends JMapViewer{
public Test()
{
addMouseListener(new DefaultMapController(this) {
public void mouseClicked(MouseEvent e){
Point start = e.getPoint();
System.out.println(e.getPoint());
}
});
}
protected void paintComponent(Graphics g){super.paintComponent(g);}
public static void main (String [] args){
JFrame jf = new JFrame();
jf.setSize(800, 600);
Test t= new Test();
jf.add(t);
jf.setVisible(true);
}
}
Running the code, after the left mouse button is pressed, the method mouseClicked() gets called multiple times (2x). After the replacement
addMouseListener(new DefaultMapController(this) {
with
addMouseListener(new MouseAdapter() {
the code works correctly, the method gets called only 1x. Where is the problem? Is it a bug inside the library or the syntax is wrong or unsafe? How to avoid this issue? Thanks for your help.