Skip to content
Advertisement

how to add an object in java lang to the fxml file

im trying to make a game I have made a grid of cells in a java class and another part of the UI in the fxml file but when I run it ide gives:

JavaFX Application Thread” java.lang.RuntimeException: java.lang.reflect.InvocationTargetException and points out to the array containg the cells once and many time to the constructor plz help

here is the usergrid class containing the cells class mouse gesture and actual grid note that mouse gesture class is not complete as I`m trying to solve this

package sample;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

import java.lang.reflect.InvocationTargetException;

public class userGrid extends Pane {
    static int  row;
    static int column;

    static int height;
    static int width;

    static boolean star;
    static boolean wall;

    @FXML
    StackPane gridRoot;
    gridCell [][] boardCells;
    userGrid actualGrid = new userGrid();
    public  userGrid() {
        try {
            actualGrid.row = DataModel.row;
            actualGrid.column = DataModel.column;

            actualGrid.star = false;
            actualGrid.wall = false;

            actualGrid.width = DataModel.gridWidth;
            actualGrid.height = DataModel.gridHeight;

            actualGrid.boardCells = new gridCell[row][column];

            MouseGesture M = new MouseGesture();
            for (int y = 0; y < actualGrid.row; y++) {
                for (int x = 0; x < actualGrid.column; x++) {
                    gridCell cell = new gridCell(y, x, false, false);
                    M.paint(cell);
                    actualGrid.add(cell, row, column);

                }
            }
            gridRoot.getChildren().add(actualGrid);
        }catch (Exception e){
            e.getCause();
            System.out.println("check");
            e.printStackTrace();
        }

    }
    public void add(gridCell cell,int row,int column){
        int cellWidth = userGrid.width/userGrid.column;
        int cellHeight = userGrid.height/userGrid.row;
        int x = cellWidth*column;
        int y= cellHeight*row;

        boardCells[x][y] = cell;
        cell.setPrefSize(cellWidth,cellHeight);
        cell.setTranslateX(x);
        cell.setTranslateY(y);
        getChildren().add(cell);

    }
    public gridCell getCell(int x,int y){ return boardCells[x][y]; }

    public class gridCell extends StackPane{
        int row;
        int column;
        boolean star;
        boolean wall;
        public gridCell(int row,int column,boolean star,boolean wall){
            this.star=star;
            this.wall=wall;
            this.row=row;
            this.column=column;
            getStyleClass().add("cell");
        }
        public void makeStar(){
            getStyleClass().remove("cell-highlight");
            getStyleClass().add("cell-star-highlight");
        }
        public void makeSmileyFace(int n){
            if(n==1){
                getStyleClass().remove("cell-highlight");
                getStyleClass().add("cell-smiley1-highlight");
            }else if(n==2){
                getStyleClass().remove("cell-smile2-highlight");
                getStyleClass().add("cell-smiley2-highlight");
            }
        }
        public void makeWall(){
            getStyleClass().remove("cell-wall-highlight");
            getStyleClass().add("cell-wall-highlight");
        }
        public void cellHover(){
            getStyleClass().remove("cell-hover-highlight");
            getStyleClass().add("cell-hover-highlight");
        }

        public void RemoveStar(){
            getStyleClass().remove("cell-highlight");
            getStyleClass().add("cell-star-highlight");
        }

        public void RemoveSmileyFace(int n){
            if(n==1){
                getStyleClass().remove("cell-highlight");
            }else if(n==2){
                getStyleClass().remove("cell-smile2-highlight");
            }
        }

        public void RemoveWall(){
            getStyleClass().remove("cell-wall-highlight");
        }
        public void UnhighlightHover(){
            getStyleClass().remove("cell-hover-highlight");
        }

        public String toString() {
            return this.column + "C||R" + this.row;
        }
    }

    public class MouseGesture{
        public void paint(Node node){
            if(true){
                node.hoverProperty().addListener(new ChangeListener<Boolean>(){
                  @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue){

                      System.out.println( observable + ": " + newValue);

                      if( newValue) {
                          ((gridCell) node).cellHover();
                      } else {
                          ((gridCell) node).UnhighlightHover();
                      }

                      for( String s: node.getStyleClass())
                          System.out.println( node + ": " + s);
                  }

                });


//                node.setOnMousePressed( onMousePressedEventHandler);
//                node.setOnDragDetected( onDragDetectedEventHandler);
//                node.setOnMouseDragEntered(onMouseDragEnteredEventHandler);

            }


        }



    }

}

here is the fxml file I`ve made some buttons then tried to add the actualgrid to the stackpane with fx:id of grid root

<?xml version="1.0" encoding="UTF-8"?>
<!--<?import sample.userGrid?>-->
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>


<?import javafx.scene.layout.StackPane?>
<GridPane prefHeight="400.0" prefWidth="600.0" stylesheets="@userGrid.css"
          xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="sample.userGrid">
    <VBox>
    <HBox alignment="TOP_LEFT">
        <Button fx:id="starButton" id="starButton-highlight" text="Star" ></Button>
        <Button fx:id="wallButton" id="wallButton-highlight" text="Wall" ></Button>
        <Button fx:id="smiley1Button" id="smiley1Button-highlight" text="smiley1" ></Button>
        <Button fx:id="smiley2Button" id="smiley2Button-highlight" text="smiley2" ></Button>
    </HBox>
        <StackPane fx:id="gridRoot">
            <children>

            </children>
        </StackPane>

    </VBox>

</GridPane>

here is the way I tried to call the grid note tha there is some try catch blocks as I`m trying to determine the cause of the problem

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.io.IOException;



public class initializePageController {
    @FXML
    TextField GridWidth;
    @FXML
    TextField GridHeight;
    @FXML
    TextField row;
    @FXML
    TextField column;
    @FXML
    TextField name1;
    @FXML
    TextField name2;

    Parent root;
    Stage stage;


    public void pageDimensions() throws IOException {
        int gHeight =Integer.parseInt(GridHeight.getText());
        int gWidth = Integer.parseInt(GridHeight.getText());
        int rrow = Integer.parseInt(row.getText());
        int ccolumn = Integer.parseInt(column.getText());
        DataModel.gridHeight=gHeight;
        DataModel.gridWidth=gWidth;
        DataModel.row=rrow;
        DataModel.column=ccolumn;
        try {
            System.out.println("Dimension Happened");
            //System.out.println( gHeight);
            System.out.println("AIMING FOR GRID");

            try {
                root = FXMLLoader.load(getClass().getResource("/sample/GridInitializer.fxml"));
                stage = (Stage) row.getScene().getWindow();
                Scene scene = new Scene(root, DataModel.gridWidth, DataModel.gridHeight);
                stage.setScene(scene);
                stage.setTitle("please god ");
                stage.show();
            } catch (Exception e) {

                System.out.println("grid initiakizer went wring /init.page controller");
                e.printStackTrace();
            }
        }catch (Exception ex){
            System.out.println("101");
            ex.getCause();
            System.out.println("101");
        }

    }
}

here is the data model class for containing the data

package sample;

public class DataModel {
    public static int gridHeight;
    public static int gridWidth;
    public static String name1;
    public static String name2;
    public static int row;
    public static int column;


}

and the css file used to design the code

.cell{
    -fx-background-color: rgb(38,38,38);
    -fx-border-color: red;
    -fx-border-width: 1px;
}
.cell-highlight {
    -fx-background-color:derive(blue,0.9);
}
.cell-hover-highlight {
    -fx-background-color:derive(green,0.9);
}
.cell-star-highlight{
    -fx-background-color: derive(yellow,1%);
    -fx-shape: "M 100 0 L175 200 L0 75 L200 75 L25 200 Z";
}
.cell-smily1-highlight{
 -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
    -fx-background-color: derive(red,10%);
}
.cell-smily2-highlight{
    -fx-background-color: derive(blue,10%);
    -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
}
.cell-wall-highlight{

}

and here is the complete stack trace the first three line is just a check`

action happend
Dimension Happened
AIMING FOR GRID
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 31 more
Caused by: java.lang.StackOverflowError
    at javafx.scene.Node.getScene(Node.java:932)
    at javafx.scene.Node.updateCanReceiveFocus(Node.java:8099)
    at javafx.scene.Node.setTreeVisible(Node.java:8007)
    at javafx.scene.Node.updateTreeVisible(Node.java:7998)
    at javafx.scene.Node.<init>(Node.java:2349)
    at javafx.scene.Parent.<init>(Parent.java:1295)
    at javafx.scene.layout.Region.<init>(Region.java:457)
    at javafx.scene.layout.Pane.<init>(Pane.java:124)
    at sample.userGrid.<init>(userGrid.java:26)
    at sample.userGrid.<init>(userGrid.java:25)
    at sample.userGrid.<init>(userGrid.java:25)
    at sample.userGrid.<init>(userGrid.java:25)

Advertisement

Answer

Yes, James_D get the point. I tested your code and with a few tweaks it worked. Look:

Main.java

package application;

import controller.InitializePageController;
import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

            InitializePageController startApp = new InitializePageController();
            startApp.pageDimensions();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

DataModel.java

package model;

public class DataModel {
    public static int gridHeight;
    public static int gridWidth;
    public static String name1;
    public static String name2;
    public static int row;
    public static int column;
}

UserGrid.java (fixing StackOverflow Exception)

package action;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import model.DataModel;

public class UserGrid extends Pane {

    static int row;
    static int column;

    static int height;
    static int width;

    static boolean star;
    static boolean wall;

    @FXML
    StackPane gridRoot;
    gridCell [][] boardCells;

    public void init() {

        try {
            UserGrid actualGrid = new UserGrid();
            UserGrid.row = DataModel.row;
            UserGrid.column = DataModel.column;

            UserGrid.star = false;
            UserGrid.wall = false;

            UserGrid.width = DataModel.gridWidth;
            UserGrid.height = DataModel.gridHeight;

            actualGrid.boardCells = new gridCell[row][column];

            MouseGesture M = new MouseGesture();
            for (int y = 0; y < UserGrid.row; y++) {
                for (int x = 0; x < UserGrid.column; x++) {
                    gridCell cell = new gridCell(y, x, false, false);
                    M.paint(cell);
                    actualGrid.add(cell, row, column);

                }
            }
            gridRoot.getChildren().add(actualGrid);

        } catch (Exception e){

            e.getCause();
            System.out.println("check");
            e.printStackTrace();
        }

    }

    public void add(gridCell cell,int row,int column){

        int cellWidth = UserGrid.width/UserGrid.column;
        int cellHeight = UserGrid.height/UserGrid.row;
        int x = cellWidth*column;
        int y= cellHeight*row;

        boardCells[x][y] = cell;
        cell.setPrefSize(cellWidth,cellHeight);
        cell.setTranslateX(x);
        cell.setTranslateY(y);
        getChildren().add(cell);

    }

    public gridCell getCell(int x,int y){
        return boardCells[x][y];
    }

    public class gridCell extends StackPane{

        int row;
        int column;
        boolean star;
        boolean wall;

        public gridCell(int row,int column,boolean star,boolean wall){
            this.star=star;
            this.wall=wall;
            this.row=row;
            this.column=column;
            getStyleClass().add("cell");
        }

        public void makeStar(){
            getStyleClass().remove("cell-highlight");
            getStyleClass().add("cell-star-highlight");
        }

        public void makeSmileyFace(int n){
            if(n==1){
                getStyleClass().remove("cell-highlight");
                getStyleClass().add("cell-smiley1-highlight");
            }else if(n==2){
                getStyleClass().remove("cell-smile2-highlight");
                getStyleClass().add("cell-smiley2-highlight");
            }
        }

        public void makeWall(){
            getStyleClass().remove("cell-wall-highlight");
            getStyleClass().add("cell-wall-highlight");
        }

        public void cellHover(){
            getStyleClass().remove("cell-hover-highlight");
            getStyleClass().add("cell-hover-highlight");
        }

        public void RemoveStar(){
            getStyleClass().remove("cell-highlight");
            getStyleClass().add("cell-star-highlight");
        }

        public void RemoveSmileyFace(int n){
            if(n==1){
                getStyleClass().remove("cell-highlight");
            }else if(n==2){
                getStyleClass().remove("cell-smile2-highlight");
            }
        }

        public void RemoveWall(){
            getStyleClass().remove("cell-wall-highlight");
        }

        public void UnhighlightHover(){
            getStyleClass().remove("cell-hover-highlight");
        }

        public String toString() {
            return this.column + "C||R" + this.row;
        }
    }

    public class MouseGesture{

        public void paint(Node node){

            if(true){

                node.hoverProperty().addListener(new ChangeListener<Boolean>(){

                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue){

                        System.out.println( observable + ": " + newValue);

                        if(newValue) {

                            ((gridCell) node).cellHover();

                        } else {

                            ((gridCell) node).UnhighlightHover();
                        }

                        for(String s: node.getStyleClass()){
                            System.out.println( node + ": " + s);
                        }
                    }

               });
            }
        }
    }
}

InitializerPageController.java

package controller;

import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import model.DataModel;

public class InitializePageController {
    
    @FXML
    TextField GridWidth;
    @FXML
    TextField GridHeight;
    @FXML
    TextField row;
    @FXML
    TextField column;
    @FXML
    TextField name1;
    @FXML
    TextField name2;

    Parent root;
    Stage stage = new Stage();


    public void pageDimensions() throws IOException {
      /*int gHeight =Integer.parseInt(GridHeight.getText());
        int gWidth = Integer.parseInt(GridWidth.getText());
        int rrow = Integer.parseInt(row.getText());
        int ccolumn = Integer.parseInt(column.getText());*/

        int gHeight =Integer.parseInt("50");
        int gWidth = Integer.parseInt("50");
        int rrow = Integer.parseInt("25");
        int ccolumn = Integer.parseInt("25");

        DataModel.gridHeight=gHeight;
        DataModel.gridWidth=gWidth;
        DataModel.row=rrow;
        DataModel.column=ccolumn;

        try {
            System.out.println("Dimension Happened");
            //System.out.println( gHeight);
            System.out.println("AIMING FOR GRID");

            try {
                root = FXMLLoader.load(getClass().getResource("../GridInitializer.fxml"));
                //stage = (Stage) row.getScene().getWindow();
                //Scene scene = new Scene(root, DataModel.gridWidth, DataModel.gridHeight);
                Scene scene = new Scene(root,800,800);
                stage.setScene(scene);
                stage.setTitle("please god ");
                stage.show();
                
            } catch (Exception e) {

                System.out.println("grid initiakizer went wring /init.page controller");
                e.printStackTrace();
            }
            
        } catch (Exception ex){
            System.out.println("101");
            ex.getCause();
            System.out.println("101");
        }

    }
}

GridInitializer.fxml

<?xml version="1.0" encoding="UTF-8"?>
<!--<?import sample.userGrid?>-->
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>


<?import javafx.scene.layout.StackPane?>
<GridPane prefHeight="400.0" prefWidth="600.0" stylesheets="userGrid.css"
          xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="action.UserGrid">
    <VBox>
    <HBox alignment="TOP_LEFT">
        <Button fx:id="starButton" id="starButton-highlight" text="Star" ></Button>
        <Button fx:id="wallButton" id="wallButton-highlight" text="Wall" ></Button>
        <Button fx:id="smiley1Button" id="smiley1Button-highlight" text="smiley1" ></Button>
        <Button fx:id="smiley2Button" id="smiley2Button-highlight" text="smiley2" ></Button>
    </HBox>
        <StackPane fx:id="gridRoot">
            <children>

            </children>
        </StackPane>

    </VBox>

</GridPane>

userGrid.css

.cell{
    -fx-background-color: rgb(38,38,38);
    -fx-border-color: red;
    -fx-border-width: 1px;
}
.cell-highlight {
    -fx-background-color:derive(blue,0.9);
}
.cell-hover-highlight {
    -fx-background-color:derive(green,0.9);
}
.cell-star-highlight{
    -fx-background-color: derive(yellow,1%);
    -fx-shape: "M 100 0 L175 200 L0 75 L200 75 L25 200 Z";
}
.cell-smily1-highlight{
 -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
    -fx-background-color: derive(red,10%);
}
.cell-smily2-highlight{
    -fx-background-color: derive(blue,10%);
    -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
}
.cell-wall-highlight{

}

And finally the project structure

Project structure.

Hope it helps!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement