JavaFX FXML:How to create new instances of a self made fxml to VBox

◇◆丶佛笑我妖孽 提交于 2021-01-29 06:17:04

问题


I created a card like pane so that I can add data to it and create a page with many cards according to data (much like a card layout in mobile applications) however, I don't know how to add new instances of this to VBox. I tired with and without a loop and still didn't work. It Keeps Giving the Following Error:

javafx.fxml.LoadException: 
/C:///////bin/application/HotelReservation.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = VBox[id=vboxData]
at javafx.scene.Parent$2.onProposedChange(Parent.java:435)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at application.HotelReservationController.initialize(HotelReservationController.java:45)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more

Here is the FXML File For the Card like Pane:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane fx:id="cardAnchor" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox fx:id="cardHBox" prefHeight="152.0" prefWidth="584.0">
         <children>
            <ImageView fx:id="cardPhoto" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
            <VBox prefHeight="200.0" prefWidth="100.0">
               <children>
                  <Text fx:id="cardTitle" strokeType="OUTSIDE" strokeWidth="0.0" text="Ttile:" wrappingWidth="385.9830722808838">
                     <font>
                        <Font name="System Bold" size="31.0" />
                     </font>
                  </Text>
                  <HBox prefHeight="18.0" prefWidth="386.0">
                     <children>
                        <Text fx:id="cardLocation" strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" wrappingWidth="211.9830722808838">
                           <font>
                              <Font name="System Bold" size="15.0" />
                           </font>
                        </Text>
                        <Text fx:id="cardRating" strokeType="OUTSIDE" strokeWidth="0.0" text="Rating:" wrappingWidth="172.9830722808838">
                           <font>
                              <Font name="System Bold" size="15.0" />
                           </font>
                        </Text>
                     </children>
                  </HBox>
                  <HBox prefHeight="100.0" prefWidth="200.0">
                     <children>
                        <Text fx:id="cardDescription" strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="308.9830722808838" />
                        <Button fx:id="cardDetails" mnemonicParsing="false" prefHeight="89.0" prefWidth="79.0" text="Details" textAlignment="JUSTIFY" />
                     </children>
                  </HBox>
               </children>
            </VBox>
         </children>
      </HBox>
   </children>
</AnchorPane>

and here is the FXML for where the VBox is located:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.HotelReservationController">
   <children>
      <SplitPane dividerPositions="0.14630681818181818" layoutX="-2.0" layoutY="-2.0" orientation="VERTICAL" prefHeight="706.0" prefWidth="708.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="85.0" prefWidth="162.0" style="-fx-background-color: brown;">
               <children>
                  <HBox layoutX="2.0" prefHeight="100.0" prefWidth="248.0" spacing="2.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="75.0">
                     <children>
                        <Button mnemonicParsing="false" style="-fx-background-color: #EE854E;" text="Home" />
                        <Button mnemonicParsing="false" style="-fx-background-color: #EE854E;" text="Hotels" />
                        <Button mnemonicParsing="false" style="-fx-background-color: #EE854E;" text="Cars" />
                        <Button mnemonicParsing="false" style="-fx-background-color: #EE854E;" text="Trips" />
                     </children>
                  </HBox>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="508.0" prefWidth="673.0" style="-fx-background-color: orange;">
               <children>
                  <ScrollPane fx:id="scrollPaneContent" layoutX="-1.0" layoutY="-2.0" prefHeight="605.0" prefWidth="708.0" style="-fx-background-color: orange;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <content>
                        <VBox fx:id="vboxData" prefHeight="176.0" prefWidth="705.0" style="-fx-background-color: orange;" />
                     </content></ScrollPane>
               </children></AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

and here is how I implemented it in java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;


public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        try {
            AnchorPane root =(AnchorPane) FXMLLoader.load(Main.class.getResource("/application/HotelReservation.fxml"));
            
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Hotel Reservation");
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
        
    }
}

......

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class HotelReservationController implements Initializable{
@FXML
    private ScrollPane scrollPaneContent;
    @FXML
    private VBox vboxData;
    @FXML
    private AnchorPane cardAnchor;
    @FXML
    private HBox cardHBox;

    @Override
        public void initialize(URL arg0, ResourceBundle arg1) {
            // TODO Auto-generated method stub
            vboxData.setSpacing(5);
            
            vboxData.getChildren().add(cardAnchor);
    //      for (int j = 0; j < 100; j++) {
    //          vboxData.getChildren().add(cardAnchor);
    //      }
            
        }

回答1:


There is no element in HotelReservation.fxml with fx:id="cardAnchor", so cardAnchor is null in the controller, and you get the null pointer exception shown in your stack trace.

To "create instances of a FXML", you need to load the FXML. So I think what you are trying to do here is:

public class HotelReservationController implements Initializable{
    @FXML
    private ScrollPane scrollPaneContent;
    @FXML
    private VBox vboxData;
    @FXML
    private HBox cardHBox;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // unclear why you wouldn't do this in the FXML
        vboxData.setSpacing(5);

        // guessing at the path, will need to be set appropriately:
        URL cardURL = getClass().getResource("/application/Card.fxml");

        try {
            for (int j = 0; j < 100; j++) {
                Parent cardAnchor = FXMLLoader.load(cardURL);
                vboxData.getChildren().add(cardAnchor);
            }
        } catch (IOException exc) {
            exc.printStackTrace();
            System.exit(1);
        }
        
    }
}


来源:https://stackoverflow.com/questions/65340356/javafx-fxmlhow-to-create-new-instances-of-a-self-made-fxml-to-vbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!