abisheksampath
abisheksampath

Reputation: 416

create new ImageView at runtime in JavaFX with SceneBuilder

I am using javaFX in Eclispe to create a GUI application. I use SceneBuilder to edit the graphical parts. The GUI is linked with a Controller class.

I have a button in an anchorPane, and no other elements. What i want to happen is, when i click on the button, i want to load an image "sample.png" from filesystem, and create a new ImageView and display it.

Each time I click an image, I want to create a new ImageView next to the previous one, and display "sample.png" on it.

I know how to load the image and display in ImageView. BUt, i'm not able to figure out the part when I need to dynamically create new ImageViews and place them next to the existing ImageView.

Any pointers/ideas are appreciated :)

Upvotes: 0

Views: 734

Answers (2)

Zephyr
Zephyr

Reputation: 10253

First, create a pane where you want the images to appear. It sounds like a FlowPane would be ideal for your situation.

Then, just add a new ImageView to the pane whenever you click the button.

btnAddImage.setOnAction(event -> {
    paneImages.getChildren().add(
        new ImageView("filename"));
} 

Upvotes: 1

You could create a GridPane and use:

gridpane.add(imageview, col, row);

This will add the ImageView to the indicated cell or you just may want to do:

RowConstraints col = new RowConstraints();
gridpane.getRowConstraints().add(col);
gridpane.add(imageview);

This will create a new col and then you add the view to that col.

Upvotes: 0

Related Questions