Alberto Carmona Alberto Carmona - 6 months ago 15
Java Question

Display images dynamically in a scrollPane

I want to add multiple images in a scrollpane when clicking a button. Now when I click the button a new rectangle is created and fitted with a new image.

The problem is the scrollbar is not displaying when a new image is added.

Here is the code:

@FXML
public void handleButtonPause(){
Rectangle r = new Rectangle();

if(counterIm==0){
//Ap1.setMinSize(Sp1.getWidth(), Sp1.getHeight() - 10);
r.setY(Sp1.getHeight() *((double)1/36));
r.setWidth(Sp1.getWidth()*0.75);
rectWidth=r.getWidth();
r.setHeight(Sp1.getHeight()/6);
rectHeight=r.getHeight();
}
else {
r.setY(Sp1.getHeight()*((7*counterIm+1)/36));
r.setWidth(rectWidth);
r.setHeight(rectHeight);
}
r.setX(Sp1.getWidth() / 10);
r.setArcWidth(20);
r.setArcHeight(20);

Ap1.getChildren().add(r);

Image image = new Image(new File("C:\\Users\\Manuel\\Desktop\\error.png").toURI().toString());
r.setFill(new ImagePattern(image));

Sp1.setVvalue(1);

counterIm=counterIm+1;

Answer

I don't know how your composition looks like.

A good approach to put the images into a VBox and put the VBox into the scrollpane.

And check that you have set the VBar Policy of your srollpane:

srollbar.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);

You can also set the maximal height of the VBox to double maximum to make it sure it is growing always:

vbox.setMaxHeight(Double.MAX_VALUE);
Comments