I'm using geotools v14.3. Until now I've been using ZoomInAction() to add a zoom in button to my loaded map. Now I want to add a custom toolbar for zooming, panning, resetting and other general functions.

I checked this note and check the mapPane setState() function. But unfortunately I found that my version of geotools doesn't support setState() on mapPane (JMapPane). I'm asking this as I've been unable to find another tutorial to achieve custom actions.

Is there any other way to achieve zoom in, zoom out, pan, reset, zoom to region, zoom selected area... etc, in a custom action?

My current custom action class is like this.

    private class MessageAction extends AbstractAction {

    private Component parentComponent;
    private String message;
    private String name;

    public MessageAction(Component parentComponent, String name, Icon icon, String message) {
        super(null, icon);
        putValue(Action.SHORT_DESCRIPTION, name);
        this.message = message;
        this.name = name;
        this.parentComponent = parentComponent;

    public void actionPerformed(ActionEvent actionEvent) {
        // map zooming, resetting, panning ... etc
        // mapPane.setState(...) doesn't work here.


1 Answer 1


That note is over 7 years old so things have moved on a little since then :-)

For current best practice you should look at the org.geotools.swing.tool package in the unsupported swing module. As far as I can see the idiom is to work directly on the MapPane passed in at construction.

  • I've followed your answer and added the following code, for testing. Thank you. ZoomOutTool abs = new ZoomOutTool(); mapPane.setCursorTool(abs); abs.setZoom(AbstractZoomTool.DEFAULT_ZOOM_FACTOR); With this, I can click on the map to zoom and also select a region to zoom. What about, clicking on the button and the map immediately zoom in/out by a given amount? This is like quick zoom feature, without clicking on the map. How can we achieve that feature?
    – tybandara
    Apr 29, 2016 at 13:44
  • did you try just clicking? That is the behaviour of those tools
    – Ian Turton
    Apr 29, 2016 at 15:38
  • Yes. That worked. My question was, without clicking on the map, just by clicking the button, is it possible to zoom the map.
    – tybandara
    Apr 29, 2016 at 15:52
  • you would need to modify the action handler but it would annoy me if every time I selected a tool it fired
    – Ian Turton
    Apr 29, 2016 at 15:53
  • Thanks, I'm actually thinking of a feature where a click of a button will automatically zoom the map center.
    – tybandara
    Apr 29, 2016 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.