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