gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:36 27 4
gpt4 key购买 nike

正如标题所述,构建表并将所有内容添加到表的初始化方法与 FXMLLoader 之间存在一定的冲突,FXMLLoader 应该为弹出窗口加载 FXML。

我的代码:

主要:

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>

Controller 。

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 - 工作,如果没有 - 表格会显示,但弹出的按钮不会,似乎存在冲突

最佳答案

fxml2.fxml中,您有:fx:controller =“Controller”

分配的 Controller 通过以下方式调用fxml2.fxml:
父 popUpFenster = FXMLLoader.load(getClass().getResource("fxml2.fxml"));

因此,fxml2 调用Controller,然后Controller 调用fxml2 .......

关于Javafx/FXML : conflict between initialize() method and FXML Loader:Initialize: NullPointerException, FXML.LoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com