2

I would like to delete fxml (added as a new column) from gridpane. For now, after clicking button, method addNextToCompare adds one column to mainGridPaneCarCompare and insert in that place new fxml from FXMLLoader.

In my deleteColumn method I've tried mainGridPaneCarCompare.getChildren().removeAll(...); but it returns null pointer exception. Method getChildren.clear() leaves empty space in mainGridPane. Is my plan wrong from the begining? Should I create List of fxml's and remove items severally?

Main class:

public class Main extends Application {
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    try {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/resources/mainScene.fxml"));
        new GridPane();
        GridPane tempGridPane;
        tempGridPane = loader.load();

        Scene scene = new Scene(tempGridPane);
        primaryStage.setScene(scene);
        primaryStage.show();

    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

}

Controller for both fxml's:

public class Controller {
@FXML
private GridPane mainGridPaneCarCompare;
@FXML
private AnchorPane addCarAnchorPane;
@FXML
private Button addCarButton;
@FXML
private GridPane nextColumnGridPane;
@FXML
private void initialize() {
}
@FXML
public void addNextToCompare() {
    //move anchorPane with button addNextCar one column to right
    if (GridPane.getColumnIndex(addCarAnchorPane) < 4) {
        GridPane.setColumnIndex(addCarAnchorPane, GridPane.getColumnIndex(addCarAnchorPane) + 1);
    } else {
        addCarButton.setDisable(true);
    }
    //load new fxml
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/resources/scene2.fxml"));
    GridPane temporaryGridPane = new GridPane();
    try {
        temporaryGridPane = loader.load();
    } catch (Exception e) {
        System.out.println(e);
    }
    //insert nextCarCompareWindow.fxml
    mainGridPaneCarCompare.add(temporaryGridPane, GridPane.getColumnIndex(addCarAnchorPane) - 1, 0, 1, 6);
}

@FXML
public void deleteColumn() {
    try {
        System.out.println("inside nextgridcarcompare: " + nextColumnGridPane.getChildren());
        nextColumnGridPane.getChildren().clear();
    } catch (Exception e) {
        System.out.println(e);
    }
}

}

MainScene.fxml:

<GridPane fx:id="mainGridPaneCarCompare" gridLinesVisible="true" hgap="5.0" vgap="5.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  <columnConstraints>
      <ColumnConstraints prefWidth="250.0" />
      <ColumnConstraints />
  </columnConstraints>
  <rowConstraints>
      <RowConstraints prefHeight="200.0" />
      <RowConstraints prefHeight="50.0" />
      <RowConstraints prefHeight="180.0" />
      <RowConstraints prefHeight="150.0" />
      <RowConstraints prefHeight="150.0" />
      <RowConstraints prefHeight="100.0" />
  </rowConstraints>
  <children>
      <AnchorPane prefHeight="200.0" prefWidth="250.0">
          <children>
              <ImageView fitHeight="140.0" fitWidth="220.0" layoutX="15.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true" />
              <HBox layoutX="15.0" layoutY="170.0" spacing="5.0">
                  <children>
                      <Button mnemonicParsing="false" text="Change car" />
                      <Button mnemonicParsing="false" text="Edit" />
                  </children>
              </HBox>
              <Label fx:id="selectedCarLabel" layoutX="102.0" layoutY="5.0" text="Title" />
          </children>
      </AnchorPane>
      <AnchorPane fx:id="addCarAnchorPane" GridPane.columnIndex="1">
          <children>
              <Button fx:id="addCarButton" layoutX="96.0" layoutY="87.0" mnemonicParsing="false" onAction="#addNextToCompare" text="Add car" AnchorPane.bottomAnchor="88.0" AnchorPane.leftAnchor="96.0" AnchorPane.rightAnchor="97.0" AnchorPane.topAnchor="87.0" />
          </children>
      </AnchorPane>
      <AnchorPane prefWidth="250.0" GridPane.rowIndex="1">
          <children>
              <ListView prefWidth="170.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
          </children>
      </AnchorPane>
      <AnchorPane GridPane.rowIndex="2">
          <children>
              <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
          </children>
      </AnchorPane>
      <AnchorPane GridPane.rowIndex="3">
          <children>
              <ListView layoutY="177.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
          </children>
      </AnchorPane>
      <AnchorPane GridPane.rowIndex="4">
          <children>
              <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
          </children>
      </AnchorPane>
      <AnchorPane GridPane.rowIndex="5">
          <children>
              <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
          </children>
      </AnchorPane>
  </children>
</GridPane>

SecondScene.fxml

<GridPane fx:id="nextColumnGridPane" accessibleRole="NODE" gridLinesVisible="true" hgap="5.0" vgap="5.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
    <ColumnConstraints prefWidth="250.0" />
</columnConstraints>
<rowConstraints>
    <RowConstraints minHeight="-Infinity" prefHeight="200.0" />
    <RowConstraints prefHeight="50.0" />
    <RowConstraints prefHeight="180.0" />
    <RowConstraints prefHeight="150.0" />
    <RowConstraints prefHeight="150.0" />
    <RowConstraints prefHeight="100.0" />
</rowConstraints>
<children>
    <AnchorPane>
        <children>
            <ImageView fitHeight="140.0" fitWidth="220.0" layoutX="15.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true" />
            <HBox layoutX="15.0" layoutY="170.0" spacing="5.0">
                <children>
                    <Button fx:id="deleteButton" mnemonicParsing="false" onAction="#deleteColumn" text="Delete" />
                    <Button mnemonicParsing="false" text="Change car" />
                    <Button mnemonicParsing="false" text="Edit" />
                </children>
            </HBox>
            <Label layoutX="102.0" layoutY="5.0" text="Title" />
        </children>
    </AnchorPane>
    <AnchorPane GridPane.rowIndex="1">
        <children>
            <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
        </children>
    </AnchorPane>
    <AnchorPane GridPane.rowIndex="2">
        <children>
            <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
        </children>
    </AnchorPane>
    <AnchorPane GridPane.rowIndex="3">
        <children>
            <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
        </children>
    </AnchorPane>
    <AnchorPane GridPane.rowIndex="4">
        <children>
            <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
        </children>
    </AnchorPane>
    <AnchorPane GridPane.rowIndex="5">
        <children>
            <ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
        </children>
    </AnchorPane>
</children>
</GridPane>

This is my first post and I hope, I express myself clearly. Thanks for any advice:)

newThomas
  • 21
  • 2
  • You basically want to do what you tried: remove the pane from `mainGridPaneCarCompare`. You get a null pointer exception, because `mainGridPaneCarCompare` is only initialized in the controller for `MainScene.fxml`, but the method `deleteColumn()` is invoked on the controller for `SecondScene.fxml`. You need to set up proper communication between the two controllers: see https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml. It will be much easier if you use a different class for each controller. – James_D Jan 25 '18 at 19:55
  • Ok, I create SecondController for SecondScene.fxml. But in this case it is necessary to create controllers for each column? Fxml will be the same but data inside will be change. – newThomas Jan 29 '18 at 10:09

0 Answers0