在同一场景的另一个 JavaFX 控制器中进行更改

Making changes in another JavaFX Controller on the same Scene

这是我的观点:

说明

整个 window 本身运行在一个名为 CartWindowController 的控制器上,产品列表本身是一个名为 CartItemComponent 的 JavaFX 自定义控件,它有自己的控制器。因此,列表中的每个项目都有自己的控制器实例,因为我以编程方式创建实例并填充 VBox。

问题

我不知道如何在用户单击由 "CartItemComponent" 控制器处理的 "X" 按钮时通知购物车Window控制器。如果有人能提醒我如何解决这个问题,我将不胜感激。

这是整个 Window 我的 FXML 的样子:

CartWindow.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" fx:id="parent" prefHeight="400.0" prefWidth="600.0" styleClass="pane" stylesheets="@../assets/userwindow.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ordermanagementsystem.controllers.CartWindowController">
    <children>
        <Button layoutX="25.0" layoutY="25.0" mnemonicParsing="false" onAction="#exitWindow" prefHeight="35.0" prefWidth="20.0" styleClass="back-button" />
        <Label layoutX="262.0" layoutY="17.0" styleClass="heading" text="Cart" />

        <ScrollPane hbarPolicy="NEVER" layoutY="97.0" prefHeight="315.0" prefWidth="600.0" styleClass="no-padding">
            <content>
              <AnchorPane prefWidth="600.0" styleClass="no-padding">
                <children>
                    <VBox fx:id="productList" layoutX="25.0" prefWidth="350.0" />
                    <AnchorPane layoutX="425.0" layoutY="25.0" prefWidth="150.0" styleClass="no-padding">
                        <children>
                            <Label layoutX="18.0" styleClass="heading-sub" text="Summary" />
                        <Label layoutX="1.0" layoutY="55.0" text="Gross:" />
                        <Label fx:id="grossTotal" alignment="CENTER_RIGHT" layoutX="47.0" layoutY="55.0" prefHeight="17.0" prefWidth="103.0" text="RM0.00" />
                        <Label layoutX="1.0" layoutY="75.0" text="Packaging:" />
                        <Label fx:id="packagingTotal" alignment="CENTER_RIGHT" layoutX="73.0" layoutY="75.0" prefHeight="17.0" prefWidth="77.0" text="RM0.00" />
                        <Label layoutX="1.0" layoutY="95.0" text="Total:" />
                        <Label fx:id="total" alignment="CENTER_RIGHT" layoutX="40.0" layoutY="95.0" prefHeight="17.0" prefWidth="110.0" styleClass="green-text" text="RM0.00" />
                        <Button layoutY="125.0" mnemonicParsing="false" onAction="#checkout" prefHeight="39.0" prefWidth="150.0" styleClass="action-button" text="Check Out" />
                        </children>
                    </AnchorPane>
                </children>
              </AnchorPane>
            </content>
        </ScrollPane>
    </children>
</AnchorPane>

CartWindowController.java

package ordermanagementsystem.controllers;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import ordermanagementsystem.cart.CartState;
import ordermanagementsystem.orders.models.OrderItem;
import ordermanagementsystem.viewcomponents.CartItemComponent;

public class CartWindowController extends ViewController implements Initializable {

    @FXML
    public Parent parent;

    @FXML
    private VBox productList;

    @FXML
    private Label grossTotal;

    @FXML
    private Label packagingTotal;

    @FXML
    private Label total;

    private CartState cartState;

    @FXML
    private void exitWindow(ActionEvent event) {
        try {
            this.openPage(this.parent, "MainWindow.fxml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @FXML
    private void checkout(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        this.cartState = CartState.getInstance();

        for (OrderItem item : this.cartState.getItems()) {
            this.productList.getChildren().add(new CartItemComponent(item));
        }
    }    

}

CartItemComponent.java:

package ordermanagementsystem.viewcomponents;

import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import ordermanagementsystem.DialogBox;
import ordermanagementsystem.cart.CartState;
import ordermanagementsystem.orders.models.OrderItem;

public class CartItemComponent extends AnchorPane {

    @FXML
    private AnchorPane frame;

    @FXML
    private TextField quantity;

    @FXML
    private Label total;

    @FXML
    private Label productName;

    private OrderItem item;

    private CartState cartState;

    public CartItemComponent(OrderItem item) {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ordermanagementsystem/views/components/CartItemComponent.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();            
        } catch (IOException exception) {
            exception.getStackTrace();
        }

        this.cartState = CartState.getInstance();
        this.item = item;

        this.setQuantity(1);
        this.quantity.setEditable(false);

        this.productName.setText(this.item.getProduct().getName());
    }

    private void setQuantity(int quantity) {
        this.quantity.setText(String.valueOf(quantity));
        this.item.setQuantity(quantity);
        this.cartState.updateItem(this.item);
        this.total.setText("RM" + String.format("%.2f", item.getProduct().getPrice() * quantity));
    }

    private int getQuantity() {
        return Integer.parseInt(this.quantity.getText());
    }

    private void updateSummary() {

    }

    @FXML
    private void add(ActionEvent event) {
        int quantity = this.getQuantity();

        if (quantity == 99) {
            DialogBox.showValidationDialog("The quantity cannot be over 99.");
        } else {
            this.setQuantity(quantity + 1);
        }
    }

    @FXML
    private void substract(ActionEvent event) {
        int quantity = this.getQuantity();

        if (quantity == 1) {
            DialogBox.showValidationDialog("The quantity cannot be below 1.");
        } else {
            this.setQuantity(quantity - 1);
        }
    }
}

你可以尝试将 CartWindowController 传递给 CartItemComponent 构造函数,然后当你需要通知 CartWindowController 时,调用方法或设置标志或触发器一场盛会,你的选择!您需要做的就是向 CartItemComponent 添加一个 CartWindowController 类型的参数,然后保存引用。