Search code examples
javaswingcardlayoutjmapviewer

JMapViewer in NetBeans: adding only map to a GUI JPanel


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!


Solution

  • 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 CardPanel are added to a JPanel having CardLayout.

    cards.add(new CardPanel("London", new Coordinate(51.5072, -0.1275)));
    cards.add(new CardPanel("Paris", new Coordinate(48.8566, 2.3522)));
    

    Each such instance has a GridLayout, but you might substitute a BorderLayout and experiment with adding the map to the CENTER and other components in the surrounding regions.

    London Paris

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import org.openstreetmap.gui.jmapviewer.Coordinate;
    import org.openstreetmap.gui.jmapviewer.JMapViewer;
    
    /**
     * @see https://stackoverflow.com/a/38934376/230513
     * @see https://stackoverflow.com/a/36392696/230513
     * @see https://stackoverflow.com/a/36243395/230513
     */
    public class CardPanel extends JPanel {
    
        private static final JPanel cards = new JPanel(new CardLayout());
        private final String name;
    
        public CardPanel(String name, Coordinate coordinate) {
            super(new GridLayout());
            this.name = name;
            JMapViewer map = new JMapViewer() {
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(320, 240);
                }
            };
            map.setDisplayPosition(coordinate, 12);
            this.add(map);
        }
    
        @Override
        public String toString() {
            return name;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    create();
                }
            });
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cards.add(new CardPanel("London", new Coordinate(51.5072, -0.1275)));
            cards.add(new CardPanel("Paris", new Coordinate(48.8566, 2.3522)));
            JPanel control = new JPanel();
            control.add(new JButton(new AbstractAction("\u22b2Prev") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.previous(cards);
                }
            }));
            control.add(new JButton(new AbstractAction("Next\u22b3") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.next(cards);
                }
            }));
            f.add(cards, BorderLayout.CENTER);
            f.add(control, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }