Skip to content

Instantly share code, notes, and snippets.

@sedj601
Created November 25, 2024 19:50
Show Gist options
  • Save sedj601/4f312127e7b0ae03291931c8510f5f78 to your computer and use it in GitHub Desktop.
Save sedj601/4f312127e7b0ae03291931c8510f5f78 to your computer and use it in GitHub Desktop.
//SedJ601
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
*
* @author blj0011
*/
public class App extends Application
{
public static final int GRID_CELL_WIDTH = 250;
public static final int GRID_CELL_HEIGHT = 250;
public static final int GRID_CELL_SPACING_HORIZONTAL = 5;
public static final int GRID_CELL_SPACING_VERTICAL = 5;
@Override
public void start(Stage primaryStage)
{
ObservableList<Product> observableList = FXCollections.observableArrayList();
for(int i = 1; i <= 100; i++) {
observableList.add(new Product(i, "Product " + i, ThreadLocalRandom.current().nextInt(100), createFakeImage(200, 150)));
}
GridView<Product> gridView = new GridView(observableList);
gridView.setCellWidth(GRID_CELL_WIDTH);
gridView.setCellHeight(GRID_CELL_HEIGHT);
gridView.setHorizontalCellSpacing(GRID_CELL_SPACING_HORIZONTAL);
gridView.setVerticalCellSpacing(GRID_CELL_SPACING_VERTICAL);
gridView.setCellFactory(gridCell -> {
return new GridCell<Product>()
{
final Label lblTitle = new Label();
final ImageView ivImage = new ImageView();
final Button btnAdd = new Button("+");
final Button btnRemove = new Button("-");
final Label lblCount = new Label();
final HBox hbButtonsRoot = new HBox(btnAdd, lblCount, btnRemove);
final VBox vbRoot = new VBox(lblTitle, ivImage, hbButtonsRoot);
{
lblTitle.setFont(new Font(20));
btnAdd.setPrefSize(25, 25);
btnRemove.setPrefSize(25, 25);
hbButtonsRoot.setSpacing(1);
hbButtonsRoot.setAlignment(Pos.CENTER);
VBox.setVgrow(hbButtonsRoot, Priority.ALWAYS);
vbRoot.setSpacing(5);
vbRoot.setAlignment(Pos.CENTER);
vbRoot.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
}
@Override
public void updateItem(Product item, boolean empty)
{
if (empty || item == null) {
setText(null);
setGraphic(null);
}
else {
lblTitle.setText(item.getName());
ivImage.setImage(item.getImage());
lblCount.setText(Integer.toString(item.getInventoryCount()));
setGraphic(vbRoot);
}
}
};
});
StackPane root = new StackPane(gridView);
Scene scene = new Scene(root, 800, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
List<Color> defaultColors = getAllDefaultColors();
private Image createFakeImage(int width, int height) {
Color currentColor = defaultColors.get(ThreadLocalRandom.current().nextInt(defaultColors.size()));
WritableImage writableImage = new WritableImage(width, height);
PixelWriter pixelWriter = writableImage.getPixelWriter();
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
pixelWriter.setColor(x, y, currentColor);
}
}
return writableImage;
}
private List<Color> getAllDefaultColors(){
List<Color> colors = new ArrayList<>();
try
{
Class clazz = Class.forName("javafx.scene.paint.Color");
if (clazz != null) {
Field[] field = clazz.getFields();
for (Field f : field) {
Object obj = f.get(null);
if(obj instanceof Color){
colors.add((Color) obj);
}
}
}
} catch (ClassNotFoundException | IllegalArgumentException | IllegalAccessException ex) {
System.out.println(ex.toString());
}
return colors;
}
}
import javafx.scene.image.Image;
import java.util.Objects;
public class Product {
private long id;
private String name;
private int inventoryCount;
private Image image;
public Product(long id, String name, int inventoryCount, Image image) {
this.id = id;
this.name = name;
this.inventoryCount = inventoryCount;
this.image = image;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getInventoryCount() {
return inventoryCount;
}
public void setInventoryCount(int inventoryCount) {
this.inventoryCount = inventoryCount;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", inventoryCount=" + inventoryCount +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment