Issue
I have the following problem: I have a rectangle, which have a large width. As you see in the Picture (1) the Rectangle width is greater then the pane width. So the part of the rectangle should be invisible or cut of like in picture (2)
Solution
I didn't understand What you exactly want but I guess this will help
import java.io.IOException;
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 TestApp extends Application {
@Override
public void start(Stage stage) throws IOException{
Rectangle rec = new Rectangle(200 , 50);
rec.setLayoutX(20);
rec.setFill(Color.BLUE);
Pane pane = new Pane();
pane.setPrefSize(200, 50);
pane.setStyle(" -fx-background-color : red");
rec.setWidth(pane.getPrefWidth() - rec.getLayoutX());
rec.layoutXProperty().addListener((observable, oldValue, newValue) -> {
rec.setWidth(pane.getWidth() - newValue.doubleValue());
});
pane.getChildren().add(rec);
stage.setScene(new Scene(new Pane(pane) , 400 , 100));
stage.show();
}
public static void main(String[] args) throws InterruptedException {
launch(args);
}
}
I hope that what you wanted And if you care about those colors you can clip the rectangle Have a look at this one explains the clip https://stackoverflow.com/a/15922252/14226680
or the clip in your case is
import java.io.IOException;
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 TestApp extends Application {
@Override
public void start(Stage stage) throws IOException{
Rectangle rec = new Rectangle(22222 , 50);
rec.setLayoutX(20);
rec.setFill(Color.BLUE);
Pane pane = new Pane();
pane.setPrefSize(500, 50);
pane.setStyle(" -fx-background-color : red");
Rectangle clip = new Rectangle(pane.getPrefWidth(), pane.getPrefHeight());
clip.setLayoutX(pane.getLayoutX());
clip.setLayoutX(pane.getLayoutY());
rec.setClip(clip);
pane.getChildren().add(rec);
stage.setScene(new Scene(new Pane(pane) , 400 , 100));
stage.show();
}
public static void main(String[] args) throws InterruptedException {
launch(args);
}
}
Answered By - Abood Moorad