What I have been trying to Achieve is an JavaFX application that computes the cost of the items selected through various categories which in the case are just two: Pizzas and Burgers.
When I click on button with text 'pizzas' on it the pizzas.fxml file is loaded where I select an item and thus the variable pizzatotal is incremented and then i come back to the main page. Till this step the value of variable Pizzatotal remains the same as the incremented value it was changed to. But as soon as I click on the button 'Burgers' from the main page the value of variable pizzatotal resets to 0.
I want the value of variable Pizzatotal to remain throughout my navigation between scenes in the application.
I built a total of 3 fxml files. One for the main Page which contains two buttons to navigate to the second and third page respectively.
This is the fxml code for the Main Page :
"'
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.example.demo.HelloController">
<children>
<Button layoutX="32.0" layoutY="34.0" mnemonicParsing="false" onAction="#switchtoOrderpage" prefHeight="71.0" prefWidth="114.0" text="Pizzas" />
<Button layoutX="166.0" layoutY="34.0" mnemonicParsing="false" onAction="#switchtoburger" prefHeight="71.0" prefWidth="114.0" text="Burgers" />
</children>
</AnchorPane>
The second FXML file contains a grid pane with buttons to execute different methods. Frankly speaking every button on this screen adds the cost of pizza selected to the variable : total.
The code for the second FXML file is as follows:
"'
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="50.0" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0"
prefWidth="1922.0" vgap="20.0" xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.example.demo.PizzaController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="348.1999786376953"
minWidth="10.0" prefWidth="297.0000274658203" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="433.80001220703133"
minWidth="0.0" prefWidth="298.7999633789062" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="456.0" minWidth="0.0"
prefWidth="314.99990234375" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="698.4000122070313"
minWidth="10.0" prefWidth="296.39997558593745" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="939.8000610351562"
minWidth="10.0" prefWidth="282.20002441406245" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="939.8000610351562"
minWidth="10.0" prefWidth="281.800048828125" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="238.39998779296874" minHeight="10.0"
prefHeight="238.39998779296874" vgrow="SOMETIMES" />
<RowConstraints maxHeight="188.80000000000004" minHeight="0.0"
prefHeight="47.800012207031244" vgrow="SOMETIMES" />
<RowConstraints maxHeight="312.80000000000007" minHeight="10.0"
prefHeight="228.60000000000002" vgrow="SOMETIMES" />
<RowConstraints maxHeight="312.80000000000007" minHeight="0.0"
prefHeight="52.999987792968795" vgrow="SOMETIMES" />
<RowConstraints maxHeight="494.6000122070312"
minHeight="3.5999755859375" prefHeight="262.79998779296875"
vgrow="SOMETIMES" />
<RowConstraints maxHeight="480.8000122070313" minHeight="10.0"
prefHeight="158.00001220703132" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="pizza" mnemonicParsing="false" onAction="#addpizza"
prefHeight="54.0" prefWidth="301.0" text="Paneer Pizza"
GridPane.rowIndex="1" />
<Button fx:id="addMargherita" mnemonicParsing="false"
prefHeight="56.0" prefWidth="298.0" text="Margherita Pizza"
GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button fx:id="pizza1" mnemonicParsing="false" prefHeight="54.0"
prefWidth="301.0" text="Chicken Pizza" GridPane.rowIndex="3" />
<Button fx:id="pizza2" mnemonicParsing="false" prefHeight="54.0"
prefWidth="301.0" text="Cheese Burst" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Button mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Chilli Pizza" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Button fx:id="gotomain" mnemonicParsing="false" onAction="#switchtomainpagefromPizza" prefHeight="157.0" prefWidth="266.0" text="Main page" GridPane.columnIndex="5" GridPane.rowIndex="5" />
<Button layoutX="645.0" layoutY="584.0" mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Pan Pizza" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Button mnemonicParsing="false" prefHeight="62.0" prefWidth="395.0" text="Sicilian Pizza" GridPane.columnIndex="3" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" prefHeight="47.0" prefWidth="286.0" text="Chicago-Style Pizza" GridPane.columnIndex="4" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" prefHeight="66.0" prefWidth="266.0" text="New York - Style Pizza" GridPane.columnIndex="5" GridPane.rowIndex="1" />
<Button fx:id="pizza21" mnemonicParsing="false" prefHeight="54.0" prefWidth="301.0" text="Hawaiian Pizza" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<Button layoutX="1022.0" layoutY="584.0" mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Greek Pizza" GridPane.columnIndex="4" GridPane.rowIndex="3" />
<Button mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Crust Pizza" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<Button mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Momo Pizza" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Button mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Corn Pizza" GridPane.columnIndex="5" GridPane.rowIndex="3" />
<Button mnemonicParsing="false" prefHeight="49.0" prefWidth="358.0" text="Prime Pizza" GridPane.rowIndex="5" />
<Button fx:id="pizza3" mnemonicParsing="false" prefHeight="54.0" prefWidth="301.0" text="Sauce Pizza" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Button fx:id="pizza4" mnemonicParsing="false" prefHeight="54.0" prefWidth="301.0" text="Oregano Pizza" GridPane.columnIndex="4" GridPane.rowIndex="5" />
</children>
</GridPane>
'"
The third FXML file is also exactly similar to this hence I'm not including the code to it to cut it short.
I have built 3 diff controller classes to control the Main-page, the pizzas page and the Burgers page.
The controller class for the main-page is as follows:
"'
public class HelloController extends PizzaController{
public void switchtopizzapage(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("pizzas.fxml"));
Stage stage = (Stage) (((Node) event.getSource()).getScene().getWindow());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(false);
stage.setHeight(400);stage.setWidth(600);
stage.show();
}
public void switchtoburger(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(HelloController.class.getResource("Burgers.fxml"));
Stage stage = (Stage) (((Node) event.getSource()).getScene().getWindow());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(false);
System.out.println(total);
stage.show();
}
}'"
The method 'switchtopizzapage' opens the pizza page which is the second FXML scene and the 'switchtoburger' opens the third FXML scene.
The main page looks like this:
The controller class for the pizzas.fxml file is given below:
public class PizzaController {
public int total;
public void addpizza(){
int pizzaCost = 100;
total = total + pizzaCost;
System.out.println(total);
}
public void switchtomainpagefromPizza(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Main-Page.fxml"));
Stage stage = (Stage) (((Node) event.getSource()).getScene().getWindow());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(false);
System.out.println(total);
stage.show();
}
}
The controller class for the Burgers.fxml is given below:
public class BurgerController extends PizzaController{
public void addChickenBurger(){
int ChickenBurgerCost = 100;
total = total+ ChickenBurgerCost;
System.out.println(total);
}
public void switchtomainpagefromBurger(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Main-Page.fxml"));
Stage stage = (Stage) (((Node) event.getSource()).getScene().getWindow());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(false);
stage.show();
}
}