Skip to content
Advertisement

Javafx Is there a better way to add myObjects to GridPane?

i was searching for answer and trying so many options. Finally i found way to pass my own javaxf object to GridPane. But I still think there is a better way to this than I am doing. So here is my code: Main:

package sample;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import static java.lang.String.valueOf;

public class Main extends Application {

    private My_Rectangle selectedShelf;

    Stage window;
    GridPane grid;

    @Override
    public void start(Stage primaryStage) throws Exception {
    window = primaryStage;

        BackgroundFill background_fill = new BackgroundFill(Color.PINK,
                CornerRadii.EMPTY, Insets.EMPTY);
        Background background = new Background(background_fill);

    grid = new GridPane();
    grid.setBackground(background);
    grid.setPadding(new Insets(10,10,10,10));
    grid.setVgap(10);
    grid.setHgap(10);
    grid.setAlignment(Pos.CENTER);


    for (int i = 0; i<10;i++){
        for(int y = 0; y<10;y++){
            My_Rectangle rect = new My_Rectangle(grid,i,y,new Text( valueOf(i+y)) );
        }
    }
    Scene scene = new Scene(grid, 400, 400);
    window.setScene(scene);
    window.show();

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

package sample;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

public class My_Rectangle {
    private float wide;
    private float height;
    private final Rectangle rect;
    private boolean selected;

    public  My_Rectangle( GridPane cheff, int x, int y, Text text) {
        wide = 20;
        height = 30;
        selected = false;
        rect = new Rectangle(wide,height);
        rect.setFill(Color.GREEN);
        text.setTextAlignment(TextAlignment.CENTER);

        EventHandler<MouseEvent> clickedHandler = ev -> {
            selected = !selected;
            this.selected();
            ev.consume();
        };
        rect.setOnMouseClicked(clickedHandler);
        cheff.add(rect, x ,y );
        cheff.add(text, x ,y );
    }
    public void selected() {
        if (selected) {
            rect.setFill(Color.GREEN);
            ShelfShowUp.display("KOKOS", 7);
        } else {
            rect.setFill(Color.HOTPINK);
        }
    }
}

By better way I mean, that there might be other way than passing GridPane and int x, int y indexes. I still wanna use GridPane.

Thank you for any Help I am new to this

Advertisement

Answer

As @JimD already commented, embedding a reference to the parent of a Node into a node is bad practice. There’s nothing special about the Rectangles other than the event handler which toggles the colour. Assuming that you don’t want to access the “selected” state of the Rectangles, you can ditch the custom class altogether.

The following code does everything that your sample does, except the call to the [not included] “ShelfShowUp.display(“KOKOS”, 7)” :

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import static java.lang.String.valueOf;

public class GridPaneCustom extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane grid = new GridPane();
        grid.setBackground(new Background(new BackgroundFill(Color.PINK, CornerRadii.EMPTY, Insets.EMPTY)));
        grid.setPadding(new Insets(10));
        grid.setVgap(10);
        grid.setHgap(10);
        grid.setAlignment(Pos.CENTER);

        for (int column = 0; column < 10; column++) {
            for (int row = 0; row < 10; row++) {
                Rectangle rectangle = new Rectangle(20, 30, Color.HOTPINK);
                rectangle.setOnMouseClicked(ev -> rectangle.setFill(
                        rectangle.getFill().equals(Color.HOTPINK) ? Color.GREEN : Color.HOTPINK));
                grid.add(rectangle, column, row);
                grid.add(new Text(valueOf(column + row)), column, row);
            }
        }
        primaryStage.setScene(new Scene(grid, 400, 400));
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement