在同一场景中对另一个JavaFX Controller进行更改

问题描述 投票:0回答:1

这就是我的观点:

enter image description here

说明

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

问题

当用户点击“X”按钮时,我无法弄清楚如何通知CartWindowController,该按钮由“CartItemComponent”控制器处理。如果有人能帮我解决这个问题,我将非常感激。

以下是我的FXML对整个Window的看法:

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>

cart window controller.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));
        }
    }    

}

cart item component.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);
        }
    }
}
java javafx javafx-8
1个回答
1
投票

您可以尝试的是将CartWindowController传递给CartItemComponent构造函数,然后当您需要通知CartWindowController时,您调用方法或设置标记或触发事件,您的选择!您需要做的就是将CartWindowController类型的参数添加到CartItemComponent并保存引用。

© www.soinside.com 2019 - 2024. All rights reserved.