【问题标题】:Javafx/FXML: conflict between initialize() method and FXML Loader:Initialize: NullPointerException, FXML.LoadExceptionJavafx/FXML:initialize() 方法和 FXML 加载器之间的冲突:初始化:NullPointerException,FXML.LoadException
【发布时间】:2020-01-12 12:34:10
【问题描述】:

正如标题所述,在构建表格并将所有内容添加到表格的初始化方法与应该为弹出窗口加载 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>

问题:如果您删除初始化或将其注释掉 - Neu-Button-PopUp - 工作,如果不是 - 显示表格但弹出按钮没有,好像有冲突

【问题讨论】:

  • 与您的问题无关:请学习 java 命名约定并遵守它们
  • plus:minimal reproducible example 需要错误的完整堆栈跟踪。没有,只是猜测:没有冲突(您可以通过重命名接口不需要的成员来解决) - 但在您的 fxml(或代码,取决于视角:)
  • ahh .. fxml 和控制器类之间存在严格的一对一关系 - 您不得在不同的视图中重用相同的控制器(因为每个控制器只会注入它知道的部分,将另一个注入的字段保留为空)

标签: java javafx model-view-controller initialization fxml


【解决方案1】:

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

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

所以fxml2 调用Controller 然后Controller 调用fxml2 .......

【讨论】:

  • 谢谢!我将 fxml2 更改为另一个控制器并且 - 它有效!谢谢!!!
猜你喜欢
  • 2013-02-24
  • 1970-01-01
  • 2016-04-19
相关资源
最近更新 更多