Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:32:42+00:00 2026-06-11T10:32:42+00:00

I am trying to create a canvas with javafx 2 in which the user

  • 0

I am trying to create a canvas with javafx 2 in which the user can pan and zoom. For static content my solution works, but as soon as the content gets updated while the user is panning, the mouse events stop working until the mouse button is released and pressed again.

The following is a minimal example which demonstrates the problem. If you click in a white area you can pan around, but if you start the panning with a click on the red rectangle it gets interrupted when the content is updated.

class Test extends StackPane
{
    private Timer timer = new Timer();
    private Rectangle rect;
    private double pressedX, pressedY;

    public Test()
    {
        setMinSize(600, 600);
        setStyle("-fx-border-color: blue;");

        timer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                Platform.runLater(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        if (rect != null)
                            getChildren().remove(rect);

                        rect = new Rectangle(10, 10, 200, 200);
                        rect.setFill(Color.RED);
                        getChildren().add(rect);
                    }
                });
            }
        }, 0, 100);

        setOnMousePressed(new EventHandler<MouseEvent>()
        {
            public void handle(MouseEvent event)
            {
                pressedX = event.getX();
                pressedY = event.getY();
            }
        });

        setOnMouseDragged(new EventHandler<MouseEvent>()
        {
            public void handle(MouseEvent event)
            {
                setTranslateX(getTranslateX() + event.getX() - pressedX);
                setTranslateY(getTranslateY() + event.getY() - pressedY);

                event.consume();
            }
        });
    }
}

public class TestApp extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        Scene scene = new Scene(new Test());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

I’m on Windows 8 64 bit with JDJ7u7.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:32:43+00:00Added an answer on June 11, 2026 at 10:32 am

    Well I think your code is OK, seems to run fine for me and operate without any clear issue using jdk7u7 on windows 7.

    I think maybe you want to call rect.setMouseTransparent(true), so that the rectangles don’t catch the clicks. The mouse transparency thing isn’t to do with the adding and removing of rectangles, it’s just the way that picking works in JavaFX.

    You may want to consider placing your Test node in a pannable ScrollPane rather than implementing the panning yourself – might need to wrap it in a Group to get the appropriate behaviour. But the code you have is simple and seems to work fine, so perhaps use of a ScrollPane is unnecessary and may even confuse things more.

    There is a Canvas class in Java, which is something different from what you have, so calling your node something other than Canvas is probably a good idea.

    Not to do with your question, but using a Timeline is my preferred way to handle animation rather than a Timer, though the Timer method will still work as long as you correctly use Platform.runLater as you are doing.

    Here’s a modified sample using a Timeline and .mouseTransparent() with an added frame counter so that it is clear that animation is happening.

    import javafx.animation.*;
    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.beans.property.*;
    import javafx.event.*;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    class Test extends StackPane {
      private Rectangle rect;
      private double pressedX, pressedY;
      private LongProperty frame = new SimpleLongProperty();
    
      public Test() {
        setMinSize(600, 600);
        setStyle("-fx-border-color: blue;");
        Label count = new Label();
        count.textProperty().bind(Bindings.convert(frame));
        getChildren().add(count);
        count.setMouseTransparent(true);
    
        setOnMousePressed(new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            pressedX = event.getX();
            pressedY = event.getY();
          }
        });
    
        setOnMouseDragged(new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            setTranslateX(getTranslateX() + event.getX() - pressedX);
            setTranslateY(getTranslateY() + event.getY() - pressedY);
    
            event.consume();
          }
        });
    
        Timeline t = new Timeline(new KeyFrame(Duration.millis(100), new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent event) {
            frame.set(frame.get() + 1);
    
            if (rect != null) {
              getChildren().remove(rect);
            }
    
            rect = new Rectangle(10, 10, 200, 200);
            rect.setFill(Color.RED);
            rect.setMouseTransparent(true);
            getChildren().add(0, rect);
          }
        }));
        t.setCycleCount(Timeline.INDEFINITE);
        t.play();
      }
    }
    
    public class TestApplication extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(Stage stage) {
        stage.setScene(new Scene(new Test()));
        stage.show();
      }
    }
    

    Sample program output

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create zoom in/out buttons on a canvas. The zooming works, however
I am trying to create a wave animation in my canvas, but it works
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying to create a canvas with scroll bars. Can anyone help me
I am trying to create a Flash application which will allow a user to
I am trying to create a canvas in Android where we can use it
I'm trying to create a canvas script that visually draws a cubic bezier-curve, but
I'm trying to create a canvas for drawing different objects. I've created zoom and
I'm trying to create a small 2D game in Javascript/Canvas which consists of several
I'm trying to create a starburst effect using the canvas, but the segment lines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.