adding images to a gridpane javafx

非 Y 不嫁゛ 提交于 2019-12-02 10:22:14

问题


I'm adding a list of images from a directory using an arraylist.When images are added,my ScrollPane gets crowded.How can I keep spacings between images ?

here's my code

File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
hb = new HBox();

for (File file1 : filelist1) {
     filelist2.add(file1);
}

System.out.println(filelist2.size());
gridpane.setPadding(new Insets(50,50,50,50));
gridpane.setHgap(20);
gridpane.setVgap(20);

int imageCol = 0;
int imageRow = 0;

for (int i = 0; i < filelist2.size(); i++) {
    System.out.println(filelist2.get(i).getName());
    image = new Image(filelist2.get(i).toURI().toString());
    pic = new ImageView();
    pic.setFitWidth(130);
    pic.setFitHeight(130);
    pic.setImage(image);
    hb.getChildren().add(pic);
    gridpane.add(pic, imageCol, imageRow );

    imageCol++;

    // To check if all the 4 images of a row are completed
    if(imageCol > 2){
      // Reset Column
      imageCol=0;
      // Next Row
      imageRow++;
}


回答1:


Try using HBox and VBox.

Basically, they are like little containers where you store your stuff and you can add gaps into it!

HBox ab = new HBox(10); <--The 10 is adding space (Answer to your question)

If you want to add stuff into HBox, simply write

ab.getChildren().addAll(your content here);


来源:https://stackoverflow.com/questions/25374578/adding-images-to-a-gridpane-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!