簡體   English   中英

Javafx/FXML:initialize() 方法和 FXML Loader 之間的沖突:Initialize: NullPointerException, FXML.LoadException

[英]Javafx/FXML: conflict between initialize() method and FXML Loader:Initialize: NullPointerException, FXML.LoadException

正如標題所述,構建表格並將所有內容添加到表格的 initialize 方法與應該為彈出窗口加載 FXML 的 FXMLLoader 之間存在一定的沖突。

我的代碼:

主要的:

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


public class Main extends Application {

    private TableView<Artikel> table;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
    stage.setTitle("Table View Sample");
    stage.setWidth(1250);
    stage.setHeight(1000);

    Parent root = FXMLLoader.load(getClass().getResource("fxml1.fxml"));

    Scene scene1 = new Scene(root,300,300);


    stage.setScene(scene1);
    stage.show();

}

FXML 1.

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.event.ActionEvent?>
<?import javafx.geometry.Pos?>
<?import javafx.collections.FXCollections ?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.StackPane?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="Controller" stylesheets="stylesheet.css" spacing="4" fx:id="idScene" >

<TableView fx:id="idTable" >

</TableView>

 <HBox alignment="TOP_RIGHT">
        <Button fx:id="idNew" text ="Neu" onAction="#onNew"/>

</HBox>



</VBox>

控制器。

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;



public class Controller implements Initializable  {

    @FXML
    private Button idNew;
    @FXML
    private TableView idTable;


    public void onNew(ActionEvent event) throws IOException {
        Parent popUpFenster = FXMLLoader.load(getClass().getResource("fxml2.fxml"));
        Scene scene2 = new Scene(popUpFenster,500,500);

        Stage stage = new Stage();
        stage.setScene(scene2);

        stage.initModality(Modality.APPLICATION_MODAL);

        stage.showAndWait();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        TableColumn place = new TableColumn("Platz");
        TableColumn name = new TableColumn("Name");
        TableColumn weight = new TableColumn("Gewicht");
        TableColumn price = new TableColumn("Preis");
        TableColumn amount = new TableColumn("Anzahl");




        idTable.getColumns().addAll(place,name,weight,price,amount);

        final ObservableList<Artikel> data = FXCollections.observableArrayList(
                new Artikel(1,"Hallo Welt",4,5,3),
                new Artikel(1,"Hallo Welt",5,3,4),
                new Artikel(2,"Hallo Welt",4,3,1));                


        //Step : 3#  Associate data with columns  
            place.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Platz"));

            name.setCellValueFactory(new PropertyValueFactory<Artikel,String>("Name"));

            weight.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Gewicht"));

            price.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Preis"));

            amount.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Anzahl"));





        //Step 4: add data inside table
            idTable.setItems(data);


    }
    public class Artikel {

        private int Platz;
        private String Name;
        private int Gewicht;
        private int Preis;
        private int Anzahl;


        public Artikel() {
            this.Platz = 0;
            this.Name = "Hallo Welt";
            this.Gewicht= 0;
            this.Preis = 0;
            this.Anzahl = 0;
        }

        public Artikel(int Platz, String Name, int Gewicht, int Preis,int Anzahl) {
            this.Platz = Platz;
            this.Name = Name;
            this.Gewicht = Gewicht;
            this.Preis = Preis;
            this.Anzahl = Anzahl;
        }


        public int getPlatz() {return this.Platz;};
        public String getName () {return this.Name;}
        public int getGewicht() {return this.Gewicht;};
        public int getPreis() {return this.Preis;}
        public int getAnzahl() { return this.Anzahl;}
    }

}

fxml2:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.event.ActionEvent?>
<?import javafx.geometry.Pos?>
<?import javafx.collections.FXCollections ?>
<?import javafx.geometry.Insets?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="290.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="Controller" stylesheets="stylesheet.css" fx:id="idPopUp">

<HBox alignment="CENTER">
    <Label text="Platz"/>
    <TextField fx:id="idPlace" />
</HBox>


</VBox>

問題:如果您刪除 initialize 或將其注釋掉 - Neu- Button -PopUp - 工作,否則 - 顯示表格但未彈出按鈕,似乎存在沖突

fxml2.fxml你有: fx:controller="Controller"

分配的控制器通過以下方式調用fxml2.fxml
Parent popUpFenster = FXMLLoader.load(getClass().getResource("fxml2.fxml"));

所以fxml2調用Controller ,然后Controller調用fxml2 ......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM