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

时间:2019-02-18 14:55:43

标签: java javafx javafx-8

这是我的看法:

enter image description here

说明

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

问题

当用户单击“ CartItemComponent”控制器处理的“ X”按钮时,我无法弄清楚如何通知CartWindowController。如果有人可以帮助我解决该问题,我将不胜感激。

这是我整个窗口的FXML外观:

CartWindow.fxml

.desktopbackground {
  background: url("wp-content/uploads/2019/02/mobile-Girl-2.0.png") 50px bottom no-repeat, 
              url("/wp-content/uploads/2019/02/Flower-3.0.png") 100% -10% no-repeat,
              url("/wp-content/uploads/2019/02/desktop-Background-1.0.jpg") center no-repeat;
  background-attachment: fixed;
  background-size:515px,555px,cover;    
}

CartWindowController.java

<?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>

CartItemComponent.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));
        }
    }    

}

1 个答案:

答案 0 :(得分:1)

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