1

My problem is that I can't figure out how to clear the shopping cart when the user has ordered the items he has in his/hers cart. I have two controllers one called PontunController and the other GreidaController. I have managed two use ViewSwitcher to switch between two different fxml sheets, but I am having truble with clearing the cart when the user switches back to the PontunController fxml.

package com.example.takeaway2;

import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import vinnsla.Karfa;
import vinnsla.MatsedillView;
import vinnsla.Veitingar;

public class PontunController {
    @FXML
    private Button innskraning;


    @FXML
    private ListView<Veitingar> fxkarfa;

    @FXML
    private ListView<Veitingar> fxmsedill;

    @FXML
    private Button baeta;

    @FXML
    private Button eyda;


    @FXML
    private Label heildarVerdLabel;

    @FXML
    private Label fxNafnvidskiptavinur;

    private Karfa karfa = new Karfa();

    private GreidslaController greidslaController;


    public void fxSetjaKorfuHandler(ActionEvent event) {
        Veitingar voldVeiting = fxmsedill.getSelectionModel().getSelectedItem();
        fxkarfa.getItems().addAll(voldVeiting);
        
        karfa.heildarVerdProperty().set(karfa.heildarVerdProperty().get() + voldVeiting.getVerd().get());
        heildarVerdLabel.setText(karfa.heildarVerdProperty().get() + "");

        
        

    }


    public void fxTakaUrKorfuHandler() {
        //clicked on "eyða" item gets deleted
        Veitingar voldVeiting = fxkarfa.getSelectionModel().getSelectedItem();
        if (voldVeiting != null) {
            fxkarfa.getItems().remove(voldVeiting);
            //update heildarverd
            karfa.heildarVerdProperty().set(karfa.heildarVerdProperty().get() - voldVeiting.getVerd().get());
            heildarVerdLabel.setText(karfa.heildarVerdProperty().get() + "");
        }

    }

    public void fxInnskraningHandler() {
        ViewSwitcher.switchTo(View.LOGIN);
    }


    public void fxGreidaHandler() {
        // switch to the Greidsla view
        ViewSwitcher.switchTo(View.ABOUT);
    }

   
    public void clearkarfa() {
        fxkarfa.getItems().clear();
    }


    public void initialize() {
        //no item is selected
        baeta.disableProperty().bind(Bindings.isEmpty(fxmsedill.getSelectionModel().getSelectedItems()));
        //bind connect eyda and msedill
        eyda.disableProperty().bind(Bindings.isEmpty(fxkarfa.getSelectionModel().getSelectedItems()));

        //displey items in matsedill listview 
        MatsedillView matsedillView = new MatsedillView();
        ObservableList<Veitingar> veitingar = FXCollections.observableArrayList(matsedillView.getMatsedill().getVeitingar());
        fxmsedill.setItems(veitingar);

        

    }


}

the other controller:

package com.example.takeaway2;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class GreidslaController {

    @FXML
    private TextField fxPantaText;

    private PontunController pontunController;

    public void clearFxkarfa() {
        // get the controller for PontunController.fxml
        PontunController pontunController = (PontunController) ViewSwitcher.lookup(View.MAIN);

        // clear fxkarfa
        if (pontunController != null) {
            pontunController.clearkarfa();
        }
    }

    public void onBack(ActionEvent actionEvent) {
        ViewSwitcher.switchTo(View.MAIN);
    }


    public void onForward(ActionEvent actionEvent) {
        // set the pontunController object
        clearFxkarfa();
        // switch to the next view
        ViewSwitcher.switchTo(View.MAIN);
    }

}



my PontunController fxml:

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

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

<DialogPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="499.0" prefWidth="481.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.takeaway2.PontunController">
    <header>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="437.0" prefWidth="481.0">
            <children>
                <VBox layoutX="33.0" layoutY="58.0" prefHeight="304.0" prefWidth="142.0">
                    <children>
                        <ListView fx:id="fxmsedill" prefHeight="319.0" prefWidth="142.0" />
                    </children>
                </VBox>
                <Button fx:id="innskraning" layoutX="418.0" layoutY="8.0" mnemonicParsing="false" onAction="#fxInnskraningHandler" text="Login" />
                <VBox layoutX="302.0" layoutY="42.0" prefHeight="204.0" prefWidth="130.0">
               <children>
                  <ListView fx:id="fxkarfa" prefHeight="204.0" prefWidth="130.0" />
               </children></VBox>
                <Button layoutX="300.0" layoutY="398.0" mnemonicParsing="false" onAction="#fxGreidaHandler" prefHeight="31.0" prefWidth="141.0" text="Greiða" />
            <Button fx:id="eyda" layoutX="302.0" layoutY="253.0" mnemonicParsing="false" onAction="#fxTakaUrKorfuHandler" text="eyða" />
            <Button fx:id="baeta" layoutX="376.0" layoutY="253.0" mnemonicParsing="false" onAction="#fxSetjaKorfuHandler" text="bæta" />
            <Label fx:id="heildarVerdLabel" layoutX="302.0" layoutY="362.0" prefHeight="18.0" prefWidth="140.0" text="Label" />
            <Label fx:id="fxNafnvidskiptavinur" layoutX="33.0" layoutY="31.0" prefHeight="18.0" prefWidth="59.0" text="Label" />
            </children>
        </AnchorPane>
    </header>
    <expandableContent>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="320.0" />
    </expandableContent>
</DialogPane>

and GreidslaController fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="com.example.takeaway2.GreidslaController">
    <children>
        <TextField fx:id="fxPantaText" layoutX="56.0" layoutY="55.0" prefHeight="173.0" prefWidth="497.0" text="order"/>
        <Button layoutX="63.0" layoutY="302.0" mnemonicParsing="false" onAction="#onBack" text="tilbaka"/>
        <Button layoutX="498.0" layoutY="303.0" mnemonicParsing="false" onAction="#onForward" text="staðfesta"/>
    </children>
</AnchorPane>

View

package com.example.takeaway2;

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
public enum View {
    LOGIN("vidskiptavinur-view.fxml"),
    MAIN("pontun-view.fxml"),
    ABOUT("greidsla.fxml");

    private String fileName;

    View(String fileName) {
        this.fileName = fileName;
    }

    public String getFileName() {
        return fileName;
    }
}

ViewSwitcher:

package com.example.takeaway2;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
public class ViewSwitcher {


    private static Scene scene;
    private static Map<View, Parent> cache = new HashMap<>();
    private static final Map<View, Object> controllers = new HashMap<>();


    public static void setScene(Scene scene) {
        ViewSwitcher.scene = scene;
    }

    public static void switchTo(View view) {
        if (scene == null) {
            System.out.println("No scene was set");
            return;
        }

        try {
            Parent root;

            if (cache.containsKey(view)) {
                System.out.println("Loading from cache");

                root = cache.get(view);
            } else {
                System.out.println("Loading from FXML");

                root = FXMLLoader.load(ViewSwitcher.class.getResource(view.getFileName())
                );

                cache.put(view, root);
            }

            scene.setRoot(root);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Object lookup(View v) {
        return controllers.get(v);
    }
}

Ive tried everything, but just cant figure out how clear the list:)

reidar ja
  • 17
  • 4

1 Answers1

4

You already have the controller map defined, you just need to populate it.

Fixed code for ViewController.switchTo method. After you cache your view using cache.put(view, root);, you can write controllers.put(view, loader.getController()); after you get the controller from the loader.

public static void switchTo(View view) {
    if (scene == null) {
        System.out.println("No scene was set");
        return;
    }

    try {
        Parent root;

        if (cache.containsKey(view)) {
            System.out.println("Loading from cache");

            root = cache.get(view);
        } else {
            System.out.println("Loading from FXML");

            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(
                     ViewSwitcher.class.getResource(view.getFileName())
            );
            root = loader.load();

            cache.put(view, root);
            controllers.put(view, loader.getController());
        }

        scene.setRoot(root);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406