I'm trying to move a imgShapeHolder to a diferrent row but I can't acess GridPane setRow or setCol methods...
Here is the MainMenuController check onExitButtonEnter,this is all I can do, still i get error.
package com.ljprogramming.app.controller;
import com.ljprogramming.app.Navigation;
import com.ljprogramming.app.util.enums.ButtonType;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class MainMenuController implements Controller {
private Navigation navigation;
@FXML
private BorderPane borderPane;
@FXML
private VBox vboxCenter;
@FXML
private HBox hboxCenterChild;
@FXML
private GridPane gridButtons;
@FXML
private ImageView imgShapeHolder;
@FXML
private ImageView imgPlayButton;
@FXML
private ImageView imgOptionsButton;
@FXML
private ImageView imgExitButton;
@FXML
private HBox vboxBottom;
@FXML
private VBox vboxRight;
@FXML
private VBox vboxTop;
@FXML
private HBox hboxTopChild;
@FXML
private ImageView imgTitle;
@FXML
private VBox vboxLeft;
@FXML
void onExitButtonClick(MouseEvent event) {
System.exit(0);
}
@FXML
void onExitButtonEnter(MouseEvent event) {
int a = gridButtons.getChildren().indexOf(imgShapeHolder);
gridButtons.getChildren().remove(gridButtons.getChildren().get(a));
gridButtons.add(gridButtons.getChildren().get(a),0,1);
//returns error saying Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=10.0, vgap=0.0, alignment=BOTTOM_RIGHT
}
public void setNavigation(Navigation navigation) {
this.navigation = navigation;
}
}
I'm using Scenebuilder to design the layout, when I export I get a error saying "No injectable field found in FXML Controller class", I don't know if it has anything to do with this. I've checked the .xfml file and all fx:id are ok, and controller has all fields...
javafx error and the project structure image
.fxml generated by Scenebuilder
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="borderPane" maxHeight="600.0" maxWidth="800.0" minHeight="600.0" minWidth="800.0" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ljprogramming.app.controller.MainMenuController">
<center>
<VBox fx:id="vboxCenter" prefHeight="337.0" prefWidth="533.0" BorderPane.alignment="CENTER">
<children>
<HBox fx:id="hboxCenterChild" alignment="CENTER" prefHeight="318.0" prefWidth="550.0" VBox.vgrow="NEVER">
<children>
<GridPane fx:id="gridButtons" alignment="BOTTOM_RIGHT" hgap="10.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="353.0" prefWidth="432.0" HBox.hgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="235.0" minWidth="10.0" prefWidth="141.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="509.0" minWidth="10.0" prefWidth="432.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fx:id="imgShapeHolder" fitHeight="63.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="RIGHT">
<image>
<Image url="@/img/mainmenu/shapes/PolygonShape.png" />
</image>
</ImageView>
<ImageView fx:id="imgPlayButton" fitHeight="49.0" fitWidth="163.0" onMouseClicked="#onPlayButtonClick" onMouseEntered="#onPlayButtonEnter" onMouseExited="#onPlayButtonExit" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER">
<image>
<Image url="@/img/mainmenu/buttons/PlayButton.png" />
</image>
<viewport>
<Rectangle2D minX="2.0" minY="2.0" />
</viewport>
</ImageView>
<ImageView fx:id="imgOptionsButton" fitHeight="51.0" fitWidth="258.0" onMouseClicked="#onOptionsButtonClick" onMouseEntered="#onOptionsButtonEnter" onMouseExited="#onOptionsButtonExit" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="1">
<image>
<Image url="@/img/mainmenu/buttons/OptionsButton.png" />
</image>
</ImageView>
<ImageView fx:id="imgExitButton" fitHeight="40.0" fitWidth="146.0" onMouseClicked="#onExitButtonClick" onMouseEntered="#onExitButtonEnter" onMouseExited="#onExitButtonExit" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="2">
<image>
<Image url="@/img/mainmenu/buttons/ExitButton.png" />
</image>
</ImageView>
</children>
</GridPane>
</children>
</HBox>
</children>
</VBox>
</center>
<bottom>
<HBox fx:id="vboxBottom" alignment="CENTER" prefHeight="97.0" prefWidth="800.0" BorderPane.alignment="CENTER" />
</bottom>
<right>
<VBox fx:id="vboxRight" prefHeight="334.0" prefWidth="159.0" BorderPane.alignment="CENTER" />
</right>
<top>
<VBox fx:id="vboxTop" prefHeight="169.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<HBox fx:id="hboxTopChild" alignment="CENTER" prefHeight="130.0" prefWidth="800.0">
<children>
<ImageView fx:id="imgTitle" fitHeight="119.0" fitWidth="560.0" pickOnBounds="true" smooth="false">
<image>
<Image url="@/img/mainmenu/buttons/TitleButton.png" />
</image>
</ImageView>
</children>
</HBox>
</children>
</VBox>
</top>
<left>
<VBox fx:id="vboxLeft" prefHeight="334.0" prefWidth="232.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
There are no setRow
and setCol
methods in GridPane
.
int a = gridButtons.getChildren().indexOf(imgShapeHolder);
gridButtons.getChildren().remove(gridButtons.getChildren().get(a));
gridButtons.add(gridButtons.getChildren().get(a),0,1);
This either results in a duplicate children exception or a IndexOutOfBoundsException
since after removing a child, you retrieve another child from the updated child list or use a index that is too large, if the last child was removed.
You should use GridPane.setRowIndex
and GridPane.setColumnIndex
:
GridPane.setRowIndex(imgShapeHolder, 1);
GridPane.setColumnIndex(imgShapeHolder, 0);