Search code examples
javagwtsmartgwtgxt

How to add widgets to the grid with max row of elements


I have long AarrayList<Image> imageList. When I'm trying to put every image from this list to the Grid as result I have only latest image.

My code:

final int columnCount =3; //max images in the row
        final int rowCount = (int) Math.ceil((double) data.size()/columnCount);

    Grid grid = new Grid(rowCount, columnCount);

    for (int i=0; i< imageList.size(); i++) {
        for (int row = 0; row < rowCount; row++) {
            for (int col = 0; col < columnCount; col++) {
                grid.setWidget(row, col, imageList.get(i));
            }
        }
    }

Could you help me to resolve this issue


Solution

  • this is a logical error in your for-loop. you are always just overriding the elements with the last image in imageList

    you could try something like this:

        int row = 0;
        int col = 0;
        for (Image image : imageList) {
            grid.setWidget(row, col, image);
            col++;
            if (col > 2) {
                col = 0;
                row++;
            }
        }