简体   繁体   中英

JavaFX: Remove a child from Vbox

So I am new to JavaFX, and I am trying to do an app for school and I've been reading lots of related posts, but I can't seem to figure it how to do for my own case. Here Is a picture

应用场景

So the big screen is my main screen and the one with the red border are children of the Vbox. And for the child, I have a separate FXML and a controller. When I hit the "Close Deal" button I want to remove the specific child of the Vbox.

I know I should do parent.getChildren().remove(specific_child_node); The problem is I don't know how to get the specific_child_node for the child for which has been the "Closed Deal" button pressed. The close button is in the child's controller and the Vbox is in the main page controller

Has anyone any idea of how I could do it?

I created a small example to show how you could do it:

MainController Class:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

import java.io.IOException;

public class MainController {

    @FXML
    private VBox middleVBox;

    @FXML
    public void handleAddChildBtnClick() {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("deal.fxml"));
        GridPane root = null;
        try {
            root = loader.load();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (root == null) return;

        // Get the controller instance:
        DealController controller = loader.getController();

        // Set a reference for the parent vbox:
        controller.setParent(middleVBox);

        middleVBox.getChildren().add(1, root);
    }
}

FXML File "main.fxml":

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane alignment="center" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController">
   <children>
      <VBox style="-fx-background-color: black;">
         <children>
            <Label text="Welcome, Client" textFill="WHITE" />
         </children>
      </VBox>
      <VBox fx:id="middleVBox" GridPane.columnIndex="1">
         <children>
            <Label text="Generate Public Auction" />
            <VBox>
               <children>
                  <Label text="dfgh" textFill="#1aab0d" />
               </children>
            </VBox>
         </children>
      </VBox>
      <VBox GridPane.columnIndex="2">
         <children>
            <Button mnemonicParsing="false" onAction="#handleAddChildBtnClick" text="Add Child" />
         </children>
      </VBox>
   </children>
   <columnConstraints>
      <ColumnConstraints />
      <ColumnConstraints />
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints prefHeight="200.0" />
   </rowConstraints>
</GridPane>

DealController Class:

package sample;

import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

public class DealController {

    @FXML
    private GridPane dealRoot;

    private VBox parent;

    @FXML
    public void handleCloseDealBtnClick() {
        parent.getChildren().remove(dealRoot);
    }

    void setParent(VBox parent) {
        this.parent = parent;
    }
}

FXML File "deal.fxml":

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane fx:id="dealRoot" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DealController">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" />
      <ColumnConstraints hgrow="SOMETIMES" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints vgrow="SOMETIMES" />
    <RowConstraints vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Label text="cvbn" textFill="#008612" />
      <Button mnemonicParsing="false" onAction="#handleCloseDealBtnClick" text="Close Deal" GridPane.columnIndex="1" GridPane.rowIndex="1" />
   </children>
</GridPane>

Main Class:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM