JavaFX GridView how to get visible elements id’s

I’m trying to get the current visible cells/rows indexes of the GridView filled with images.

I found the way with taking the id’s out of the listener but when I started adding text/labels over the image and putting it as StackPane to the GridView instead of Images it stopped working (classcast exception with the (IndexedCell)).

Here is main class:



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class MainApp extends Application {
    public MyGridView myGridView;


    @Override
    public void start(final Stage primaryStage) {

        myGridView = new MyGridView();

        HBox row1 = new HBox(myGridView);
        HBox.setHgrow(myGridView, Priority.ALWAYS);

        Scene scene = new Scene(row1, 1200, 800, Color.BLACK);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }


} 

Here is MyGridView class:


import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.IndexedCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;
import org.controlsfx.control.cell.ImageGridCell;

import java.awt.*;
import java.awt.image.BufferedImage;



public class MyGridView extends GridView {
    public static final int GRID_CELL_WIDTH = 512;
    public static final int GRID_CELL_HEIGHT = 360;
    public static final int GRID_CELL_SPACING_HORIZONTAL = 5;
    public static final int GRID_CELL_SPACING_VERTICAL = 5;

    public MyGridView() {
        setCellWidth(GRID_CELL_WIDTH);
        setCellHeight(GRID_CELL_HEIGHT);
        setHorizontalCellSpacing(GRID_CELL_SPACING_HORIZONTAL);
        setVerticalCellSpacing(GRID_CELL_SPACING_VERTICAL);

        addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
            Node clickedNode = e.getPickResult().getIntersectedNode();
            IndexedCell index = ((IndexedCell) clickedNode);
            int clickedItemId = index.getIndex();
            System.out.println("Clicked id = " + clickedItemId);
        });

        addEventFilter(ScrollEvent.ANY, e -> {
            Node clickedNode = e.getPickResult().getIntersectedNode();
            System.out.println(clickedNode.getClass());
            IndexedCell index = ((IndexedCell) clickedNode);
            int clickedItemId = index.getIndex();
            System.out.println("Scrolled id = " + clickedItemId);
        });


        //scroll/click cells is working with this, comment it and uncomment next one
        setCellFactoryWorking();
//        setCellFactoryNotWorking();

        addImagesToGrid(this);

    }

    private void setCellFactoryWorking() {
        setCellFactory(gv -> new GridCell<Image>() {
            private final ImageView imageView = new ImageView();

            {
                imageView.setFitHeight(GRID_CELL_HEIGHT);
                imageView.setPreserveRatio(true);
            }

            @Override
            protected void updateItem(Image image, boolean empty) {
                super.updateItem(image, empty);
                imageView.setImage(image);
                setGraphic(imageView);

            }
        });
    }

    private void setCellFactoryNotWorking() {
        setCellFactory(gv -> new GridCell<Image>() {
            private final ImageView imageView = new ImageView();

            {
                imageView.setFitHeight(GRID_CELL_HEIGHT);
                imageView.setPreserveRatio(true);
            }

            @Override
            protected void updateItem(Image image, boolean empty) {
                super.updateItem(image, empty);
                imageView.setImage(image);
                Text text = new Text("  Lorem Ipsum");
                text.setFill(javafx.scene.paint.Color.rgb(255, 255, 255));
                StackPane pane = new StackPane();
                pane.getChildren().add(imageView);
                pane.getChildren().add(text);
                pane.setAlignment(Pos.TOP_LEFT);
                setGraphic(pane);

            }
        });
    }

    private void addImagesToGrid(GridView<Image> gridView) {
        for (int i = 1; i < 200; i++) {
            final Image image = createFakeImage(i, GRID_CELL_WIDTH, GRID_CELL_HEIGHT);
            gridView.getItems().add(image);
        }
    }


    private static Image createFakeImage(int imageIndex, int width, int height) {
        BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        for (int i = 1; i < width; i++) {
            g.setColor(new Color(i * imageIndex % 256, i * 2 * (imageIndex + 40) % 256, i * 3 * (imageIndex + 60) % 256));
            double proportion = width / height;
            g.drawRect(i, i, width - i * 2, (int) ((width - i * 2) * proportion));
        }


        return SwingFXUtils.toFXImage(image, null);
    }
}

If you switch setCellFactoryWorking() func to setCellFactoryNotWorking() you will see that reference class that mouse is hovering changed and there is no more IndexedCell exists with id’s. Actually that is kinda bad method detecting actual visible elements of the GridView, is there anything more generic that is not dependent on the cell element class types and just could tell:

  • visible rows id’s

  • visible cell’s id’s

  • if clicked cell with mouse then it’s id aswell

  • also would be nice to know the cell id on just mouse hovering on it

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật