¿Cómo pasar un valor a un método que llama a una nueva página en la clase del controlador del método?

Karishma Parmar:

Tengo un panel con 10 películas, y cada vez que el usuario hace clic en una miniatura de la película, tengo un método que va a llevar al usuario a una página de entradas donde pueden seleccionar el número de entradas que les gustaría comprar. Sin embargo, estoy tratando de encontrar la manera de pasar el objeto de película loadTicketPageen la clase Controller para la página llamada boletos, cuando llamo el objeto película de la página de la película del controlador.

public class controller {
    public void initialize() {
        film1.setImage(getFilmFromList(0).getThumbnail());
        film2.setImage(getFilmFromList(1).getThumbnail());
        film3.setImage(getFilmFromList(2).getThumbnail());
        film4.setImage(getFilmFromList(3).getThumbnail());
        film5.setImage(getFilmFromList(4).getThumbnail());
        film6.setImage(getFilmFromList(5).getThumbnail());
        film7.setImage(getFilmFromList(6).getThumbnail());
        film8.setImage(getFilmFromList(7).getThumbnail());
        film9.setImage(getFilmFromList(8).getThumbnail());
        film10.setImage(getFilmFromList(9).getThumbnail());

    }

    public void loadTicketPage(Film film) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("ticketsPage.fxml"));

            Scene scene = new Scene(root);
            Stage stage = new Stage();

            stage.initModality(Modality.APPLICATION_MODAL);

            stage.setScene(scene);
            stage.setTitle("Ticket Page");
            stage.show();

        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public void film1() {
        Double price = getFilmFromList(0).getPrice();
        String title = getFilmFromList(0).getFilmName();
        loadTicketPage(getFilmFromList(0));

    }

    public void film2() {
        Double price = getFilmFromList(1).getPrice();
        String title = getFilmFromList(1).getFilmName();
        loadTicketPage(getFilmFromList(1));
    }
}

public class TicketsPageController {
    public Label filmName;
    public Label filmDescription;

    public void initialize() {
        filmName.setText(film.getFilmTitle)
    }
}

Aquí está el archivo fxml para la ticketsPage:

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

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.SepiaTone?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="670.0" prefWidth="1000.0" style="-fx-background-color: maroon;" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.TicketsPageController">
   <children>
      <Label fx:id="lblHello" layoutX="308.0" layoutY="9.0" prefHeight="94.0" prefWidth="460.0" text="Hello! Welcome to Cinema Enterpriso" textAlignment="CENTER" textFill="WHITE" wrapText="true">
         <font>
            <Font name="Broadway" size="30.0" />
         </font>
      </Label>
      <ImageView fitHeight="141.0" fitWidth="297.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@images/University_of_Huddersfield_logo.jpg" />
         </image>
      </ImageView>
      <Button fx:id="btnOpenLogin" layoutX="770.0" layoutY="46.0" mnemonicParsing="false" onAction="#btnOpenLoginClick" style="-fx-background-color: tan;" text="Login">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnSignUp" layoutX="840.0" layoutY="46.0" mnemonicParsing="false" onAction="#btnCreateNewUser" style="-fx-background-color: tan;" text="Sign Up">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnBasket" layoutX="925.0" layoutY="46.0" mnemonicParsing="false" onAction="#btnOpenOrderSummary" style="-fx-background-color: tan;" text="Basket">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Label layoutX="35.0" layoutY="328.0" text="Please select your tickets:" textFill="WHITE">
         <font>
            <Font name="Broadway" size="24.0" />
         </font>
      </Label>
      <ComboBox fx:id="ticketType1" layoutX="51.0" layoutY="383.0" prefWidth="150.0" promptText="Ticket type" style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="Adult"> </String>
               <String fx:value="Child"> </String>
               <String fx:value="Teen"> </String>
               <String fx:value="Student"> </String>
               <String fx:value="Senior"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="ticketType2" layoutX="51.0" layoutY="437.0" prefWidth="150.0" promptText="Ticket type" style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="Adult"> </String>
               <String fx:value="Child"> </String>
               <String fx:value="Teen"> </String>
               <String fx:value="Student"> </String>
               <String fx:value="Senior"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="ticketType3" layoutX="51.0" layoutY="489.0" prefWidth="150.0" promptText="Ticket type" style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="Adult"> </String>
               <String fx:value="Child"> </String>
               <String fx:value="Teen"> </String>
               <String fx:value="Student"> </String>
               <String fx:value="Senior"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="ticketType4" layoutX="51.0" layoutY="541.0" prefWidth="150.0" promptText="Ticket type" style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="Adult"> </String>
               <String fx:value="Child"> </String>
               <String fx:value="Teen"> </String>
               <String fx:value="Student"> </String>
               <String fx:value="Senior"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="ticketType5" layoutX="51.0" layoutY="592.0" prefWidth="150.0" promptText="Ticket type" style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
          <items>
              <FXCollections fx:factory="observableArrayList">
                  <String fx:value="Adult"> </String>
                  <String fx:value="Child"> </String>
                  <String fx:value="Teen"> </String>
                  <String fx:value="Student"> </String>
                  <String fx:value="Senior"> </String>
              </FXCollections>
          </items>
      </ComboBox>
      <ComboBox fx:id="no1" layoutX="218.0" layoutY="383.0" prefHeight="31.0" prefWidth="79.0" promptText="no." style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="1"> </String>
               <String fx:value="2"> </String>
               <String fx:value="3"> </String>
               <String fx:value="4"> </String>
               <String fx:value="5"> </String>
               <String fx:value="6"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="no2" layoutX="217.0" layoutY="435.0" prefHeight="31.0" prefWidth="79.0" promptText="no." style="-fx-background-color: tan;">

         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="1"> </String>
               <String fx:value="2"> </String>
               <String fx:value="3"> </String>
               <String fx:value="4"> </String>
               <String fx:value="5"> </String>
               <String fx:value="6"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="no3" layoutX="217.0" layoutY="488.0" prefHeight="31.0" prefWidth="79.0" promptText="no." style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="1"> </String>
               <String fx:value="2"> </String>
               <String fx:value="3"> </String>
               <String fx:value="4"> </String>
               <String fx:value="5"> </String>
               <String fx:value="6"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="no4" layoutX="218.0" layoutY="542.0" prefHeight="31.0" prefWidth="79.0" promptText="no." style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="1"> </String>
               <String fx:value="2"> </String>
               <String fx:value="3"> </String>
               <String fx:value="4"> </String>
               <String fx:value="5"> </String>
               <String fx:value="6"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <ComboBox fx:id="no5" layoutX="217.0" layoutY="594.0" prefHeight="31.0" prefWidth="79.0" promptText="no." style="-fx-background-color: tan;">
         <effect>
            <SepiaTone />
         </effect>
         <items>
            <FXCollections fx:factory="observableArrayList">
               <String fx:value="1"> </String>
               <String fx:value="2"> </String>
               <String fx:value="3"> </String>
               <String fx:value="4"> </String>
               <String fx:value="5"> </String>
               <String fx:value="6"> </String>
            </FXCollections>
         </items>
      </ComboBox>
      <Label fx:id="filmName" layoutX="521.0" layoutY="123.0" text="Film" textFill="WHITE">
         <font>
            <Font name="Broadway" size="30.0" />
         </font>
      </Label>
      <Label fx:id="filmDescription" layoutX="485.0" layoutY="189.0" text="Description" textFill="WHITE">
         <font>
            <Font name="Broadway" size="24.0" />
         </font>
      </Label>
      <Button fx:id="btnAddToOrder1" layoutX="325.0" layoutY="383.0" mnemonicParsing="false" onAction="#btnAddTicketToOrder1" style="-fx-background-color: tan;" text="Add to order" textFill="MAROON">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnAddToOrder2" layoutX="325.0" layoutY="435.0" mnemonicParsing="false" onAction="#btnAddTicketToOrder2" style="-fx-background-color: tan;" text="Add to order" textFill="MAROON">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnAddToOrder3" layoutX="325.0" layoutY="489.0" mnemonicParsing="false" onAction="#btnAddTicketToOrder3" style="-fx-background-color: tan;" text="Add to order" textFill="MAROON">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnAddToOrder4" layoutX="325.0" layoutY="541.0" mnemonicParsing="false" onAction="#btnAddTicketToOrder4" style="-fx-background-color: tan;" text="Add to order" textFill="MAROON">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnAddToOrder5" layoutX="325.0" layoutY="595.0" mnemonicParsing="false" onAction="#btnAddTicketToOrder5" style="-fx-background-color: tan;" text="Add to order" textFill="MAROON">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
      <Button fx:id="btnBackHomepage" layoutX="485.0" layoutY="594.0" mnemonicParsing="false" onAction="#btnBackToHomePageClick" prefHeight="31.0" prefWidth="155.0" style="-fx-background-color: tan;" text="Back to Homepage" textFill="MAROON">
         <effect>
            <SepiaTone />
         </effect>
      </Button>
   </children>
</AnchorPane>

Aquí está el StackTrace:

Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
    ... 45 more
Caused by: java.lang.NullPointerException
    at sample.TicketsPageController.setStuff(TicketsPageController.java:54)
    at sample.TicketsPageController.initialize(TicketsPageController.java:41)
    ... 55 more

Aquí está mi TicketPageController

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.Window;

import java.io.IOException;

public class TicketsPageController {
    public Label lblHello;
    public Button btnOpenLogin;
    public Button btnSignUp;
    public Button btnBasket;
    public Label filmName;
    public Label filmDescription;
    public ComboBox ticketType1;
    public ComboBox ticketType2;
    public ComboBox ticketType3;
    public ComboBox ticketType4;
    public ComboBox ticketType5;
    public ComboBox no1;
    public ComboBox no2;
    public ComboBox no3;
    public ComboBox no4;
    public ComboBox no5;
    public Button btnAddToOrder1;
    public Button btnAddToOrder2;
    public Button btnAddToOrder3;
    public Button btnAddToOrder4;
    public Button btnAddToOrder5;
    public Button btnBackHomepage;
    private String filmTitle;

    private Film film;

    public void initialize() {
          setStuff();
//        System.out.println(filmTitle);
          filmName.setText(filmTitle);
    }

//    public void setFilm(Film film) {
//        System.out.println("in set film");
//        this.film = film;
//
//    }

    public void setFilm(Film film) {
        this.film = film;
        filmName.setText(film.getFilmName());
    }

    public void setStuff() {
        System.out.println("test");
        filmTitle = film.getFilmName();
    }

    public String printFilm(){
        return film.getFilmName();
    }

    public void btnOpenLoginClick(ActionEvent actionEvent) {
        printFilm();
    }

    public void btnCreateNewUser(ActionEvent actionEvent) {
    }

    public void btnOpenOrderSummary(ActionEvent actionEvent) {
    }

    public void btnAddTicketToOrder1(ActionEvent actionEvent) {
    }

    public void btnAddTicketToOrder2(ActionEvent actionEvent) {
    }

    public void btnAddTicketToOrder3(ActionEvent actionEvent) {
    }

    public void btnAddTicketToOrder4(ActionEvent actionEvent) {
    }

    public void btnAddTicketToOrder5(ActionEvent actionEvent) {
    }

    public void btnBackToHomePageClick(ActionEvent actionEvent) throws IOException {
        Window mainWindow = btnBackHomepage.getScene().getWindow();
        Parent newRoot = FXMLLoader.load(getClass().getResource("homepage.fxml"));
        mainWindow.getScene().setRoot(newRoot);
    }
}
Samuel Philipp:

Durante el proceso de carga de la nueva Panese puede obtener el controlador desde el FXMLLoadery pasar la película a la misma:

public void loadTicketPage(Film film){
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("ticketsPage.fxml"));

        Scene scene = new Scene(loader.load());
        Stage stage = new Stage();

        stage.initModality(Modality.APPLICATION_MODAL);

        stage.setScene(scene);
        stage.setTitle("Ticket Page");

        // get the controller and set the film
        TicketsPageController controller = loader.getController();
        controller.setFilm(film);

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

Asumiendo su apariencia controlador como el siguiente:

public class TicketsPageController {
    private Film film;

    public Label filmName;
    public Label filmDescription;

    public void setFilm(Film film) {
        this.film = film;
        filmName.setText(film.getFilmTitle());
    }
}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=214648&siteId=1
Recomendado
Clasificación