JavaFX not redrawing when deleting an object drawn on top of another object

In the code below, the green mouse click is not removed. However, if you resize the scene after clicking the mouse, the scene will be repainted and the green rectangle will disappear.

If you set the size of the green rectangle to 150/150, then some of them are immediately above the top of the panel, and it immediately disappears when you click.

  • Is this a JavaFX bug or am I missing something?
  • How can I delete the rectangle using the mouse?

My environment: Windows 7 / Java 1.8.0. 64-bit VM VM 25.0-b70.

package xschach.client;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

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

    public Main3() {}

    @Override
    public void start(Stage stage) throws Exception {
        Pane pane = new Pane();
        Rectangle rect = new Rectangle(10, 10, 200, 200);
        pane.getChildren().add(rect);
        stage.setScene(new Scene(pane, 300, 300));
        stage.show();

        Rectangle rect2 = new Rectangle(100, 100, 50, 50);
        rect2.setFill(Color.GREEN);
        pane.getChildren().add(rect2);

        pane.setOnMouseClicked(event -> pane.getChildren().remove(rect2));
    }
}

      

+4


source to share


3 answers


It seems to me that this is a mistake.

Doing some tests, what happens is the very last node (from the top), no matter how many we have, which lies within the bounds of the first, when it is removed, it does not appear in the scene graph, it is not marked as dirty, and is not called requestLayout()

.

I found another workaround as well. Just allow some (minimal) transparency for the first child and it will work ...

rect.setFill(Color.web("000000FE"));

      



And you can always put that node behind the first ...

pane.setOnMouseClicked(event -> {
     rect2.toBack();
     pane.getChildren().remove(rect2);
});

      

Anyway, consider the bug for Jira.

+3


source


I know it's a little late, but it might help someone.

If you use a method .clear()

in the list of children pane

if it forces the rendering of the view. So this solution, quite extreme, I admit, works:



pane.setOnMouseClicked(event -> {
       pane.getChildren().clear();
       pane.getChildren().add(rect);
    });

      

0


source


I found another workaround. Move the knot that is not erased back using the method toBack()

, and then move it back to its place using the method toFront()

. This seems to activate the dirty mechanism and redraw the background node correctly.

0


source







All Articles