loops - how to add imageviews to gridpane using a forloop -


here's code.this prompts exception saying "illegalargumentexception: children: duplicate children added: parent = grid hgap=5.0, vgap=5.0, alignment=top_left"

file file = new file("d:\server\server content\apps\icons");

            file[] filelist1 = file.listfiles();             arraylist<file> filelist2 = new arraylist<>();             hb = new hbox();              (file file1 : filelist1) {                 filelist2.add(file1);              }              system.out.println(filelist2.size());                  (int = 0; < 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);                      gridpane.setpadding(new insets(5));                     gridpane.sethgap(5);                     gridpane.setvgap(5);                     pic.setimage(image);                     hb.getchildren().add(pic);                    } 

adding items gridpane little different.

from docs

to use gridpane, application needs set layout constraints on children , add children gridpane instance. constraints set on children using static setter methods on gridpane class

applications may use convenience methods combine steps of setting constraints , adding children

so in example, first need decide : how many images need in 1 row ?

lets answer 4, code becomes : (there diff approaches it, writing down simplest one. can use anything, loops rows , colums being alternative ;) )

//can set once, no need keep them inside loop gridpane.setpadding(new insets(5)); gridpane.sethgap(5); gridpane.setvgap(5);  //declaring variables row count , column count int imagecol = 0; int imagerow = 0; (int = 0; < 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.add(pic, imagecol, imagerow );      imagecol++;       // check if 4 images of row completed      if(imagecol > 3){           // reset column           imagecol=0;           // next row           imagerow++;      } } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -