I am trying to learn JMapViewer, and have a map embedded in a JPanel, which is a part of CardLayout. At this point, I only want to display a map without zoom, mouse action listeners, etc. So, I created a GUI frame in NetBeans with CardLayout and a number of JPanels. Inside one of the panels, I have added another panel where the map should be located. Then I added JMapViewer.jar and JMapViewer_src.jar. Then I added the following simple code:
package viewController;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent;
import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener;
public class PanelAcars extends javax.swing.JPanel implements JMapViewerEventListener
{
public PanelAcars()
{
super();
setSize(400,400);
initComponents();
final JMapViewer map = new JMapViewer();
pnlAcarsMapView.add(map);
}
@Override
public void processCommand(JMVCommandEvent command) {
if (command.getCommand().equals(JMVCommandEvent.COMMAND.ZOOM) ||
command.getCommand().equals(JMVCommandEvent.COMMAND.MOVE)) {
//updateZoomParameters();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pnlAcarsMapView = new javax.swing.JPanel();
...
}
private javax.swing.JPanel pnlAcarsMapView;
}
The panel is empty. I have the Demo.java code and it works if I simply copy and paste it in a new project. But I would like to modify it starting from building a simple map and adding it to my panel. What am I missing?
Thank you!
Absent a complete example, it's not clear where your program goes awry. Common problems include these:
Adding the map to an unintended container.
Adding the map after calling
pack(), without revalidating the container.In general, avoid such problems by limiting the scope of the GUI editor as suggested here. In the example below, instances of
CardPanelare added to aJPanelhavingCardLayout.Each such instance has a
GridLayout, but you might substitute aBorderLayoutand experiment with adding the map to theCENTERand other components in the surrounding regions.