Search code examples
javaswinggeotools

MapPane geotools


I use Geotools, I have a main Jframe and i use JMapPan to display the map. but i have probleme with toolbar how to add cursor button and button to identify features.I have just add the zoom buttons. The source code :

    final MapContent map = new MapContent();
    map.setTitle("The Map");

    Layer rasterLayer = new GridReaderLayer(reader, rasterStyle);
    map.addLayer(rasterLayer);

    Layer shpLayer = new FeatureLayer(shapefileSource, shpStyle);
    map.addLayer(shpLayer);

    JMapPane mapPane = new JMapPane(map);

    JFrame frame = new JFrame("The Map");

    frame.setLayout(new BorderLayout());
    frame.add(mapPane, BorderLayout.CENTER);  


    JPanel buttons = new JPanel();
    JButton zoomInButton = new JButton(new ZoomInAction(mapPane));
    buttons.add(zoomInButton);

    JButton zoomOutButton = new JButton(new ZoomOutAction(mapPane));   
    buttons.add(zoomOutButton);

    JButton pamButton = new JButton(new PanAction(mapPane));       
    buttons.add(pamButton);

    //how to add cursor button and identify features button.

    frame.add(buttons, BorderLayout.NORTH);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setVisible(true);

Solution

  • The easiest way to do this is to use the provided JToolbar that comes with JMapFrame.

    JMapFrame frame = = new JMapFrame(mapContent);
    frame.enableToolBar(true);
    JToolBar toolBar = frame.getToolBar();
    

    But if you really must build your own then a quick look at the JMapFrame code gives:

    if (toolSet.contains(Tool.PAN)) {
      btn = new JButton(new PanAction(mapPane));
      btn.setName(TOOLBAR_PAN_BUTTON_NAME);
      toolBar.add(btn);
      cursorToolGrp.add(btn);
      toolBar.addSeparator();
    }
    if (toolSet.contains(Tool.INFO)) {
       btn = new JButton(new InfoAction(mapPaneif(toolSet.contains(Tool.PAN))     {
            btn = new JButton(new PanAction(mapPane));
            btn.setName(TOOLBAR_PAN_BUTTON_NAME);
            toolBar.add(btn);
            cursorToolGrp.add(btn);
            toolBar.addSeparator();
        }
        if (toolSet.contains(Tool.INFO)) {
            btn = new JButton(new InfoAction(mapPane));
            btn.setName(TOOLBAR_INFO_BUTTON_NAME);
            toolBar.add(btn);
            toolBar.addSeparator();
        }));
        btn.setName(TOOLBAR_INFO_BUTTON_NAME);
        toolBar.add(btn);
        toolBar.addSeparator();
    }