【发布时间】:2020-09-01 23:33:06
【问题描述】:
我正在尝试通过参考 YT 的一些教程来创建表格视图。当我做一个简单的表格视图和它的相关控制器时,它就可以工作了。
主类:
public class TableTest extends Application {
public TableTest() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
try {
// load the fxml file
FXMLLoader tableloader = new FXMLLoader(getClass().getResource("TestTableView.fxml"));
Parent root = tableloader.load();
TableTestController controller = tableloader.getController();
controller.initializeModel();
//load the login scene
Scene scene = new Scene(root, 800, 400);
stage.setTitle("Test Table Screen");
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
控制器类:
public class TableTestController {
@FXML private TableView<TablePost> t_posts ;
@FXML private TableColumn<TablePost, String> c_post_id ;
@FXML private TableColumn<TablePost, String> c_post_title ;
@FXML private TableColumn<TablePost, String> c_post_desc ;
@FXML private TableColumn<TablePost, String> c_post_creator ;
@FXML private TabPane TabPane;
@FXML private Tab AllPosts;
@FXML private AnchorPane t_anch;
@FXML private TableTestController t_anchController;
@FXML private Parent TAllPosts;
public void initializeModel() {
System.out.println("Starting Table Init");
c_post_id.setCellValueFactory(new PropertyValueFactory<TablePost, String>("postId"));
c_post_title.setCellValueFactory(new PropertyValueFactory<TablePost, String>("postTitle"));
c_post_desc.setCellValueFactory(new PropertyValueFactory<TablePost, String>("postDesc"));
c_post_creator.setCellValueFactory(new PropertyValueFactory<TablePost, String>("postCreator"));
t_posts.setItems(getPosts());
System.out.println("Finished Table Init");
}
public ObservableList<TablePost> getPosts() {
ObservableList<TablePost> posts = FXCollections.observableArrayList();
TablePost p1 ;
p1 = new TablePost("id1","title1","desc1","creator1");
posts.add(p1);
p1 = new TablePost("id2","title2","desc2","creator2");
posts.add(p1);
return posts;
}
}
TestTableView.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:id ="t_anch"
fx:controller="test.TableTestController">
<children>
<TableView fx:id="t_posts" layoutX="14.0" layoutY="65.0"
prefHeight="141.0" prefWidth="570.0">
<columns>
<TableColumn fx:id="c_post_id" prefWidth="78.0"
text="Post Id" />
<TableColumn fx:id="c_post_title" prefWidth="135.0"
text="Post Title" />
<TableColumn fx:id="c_post_desc" prefWidth="237.0"
text="Post Desc" />
<TableColumn fx:id="c_post_creator" prefWidth="119.0"
text="Post Creator" />
</columns>
</TableView>
</children>
</AnchorPane>
所有这些都有效,但是此刻,我将这个 fxml 注入到一个选项卡视图中,我得到了 NPE。 即,如果我将 Main 类中的第 28 行更改为引用新的基于 Tab 的 fxml,它将失败
这行得通:FXMLLoader tableloader = new FXMLLoader(getClass().getResource("TestTableView.fxml"));
这不起作用:FXMLLoader tableloader = new FXMLLoader(getClass().getResource("TestTableView2.fxml"));
TestTableView2.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="515.0"
prefWidth="714.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="test.TableTestController">
<center>
<TabPane fx:id="TabPane">
<tabs>
<Tab fx:id="AllPosts" closable="false" text="All Posts">
<content>
<fx:include fx:id="TAllPosts"
source="TestTableView.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</center>
</BorderPane>
【问题讨论】:
-
不相关:请使用 java 命名约定
-
永远不要对多个 FXML 文件使用相同的控制器类。具体的控制器类和 FXML 文件之间的关系应该是一对一的。否则,在任何给定时间推断哪些字段将是
null变得更加困难;请记住,默认情况下,FXMLLoader将创建由fx:controller指定的控制器类的 new 实例。如果您需要在控制器实例之间进行通信,无论它们是否属于同一类,请参阅Passing Parameters JavaFX FXML。 -
使用
<fx:include>为您添加的fxml创建了一个不同的控制器实例。正确的字段名称来获取包含的fxml注入的控制器实例将是TAllPostsControllernott_anchController。虽然理论上你可以为两个 fxml 使用相同的控制器类,这是一个非常糟糕的主意:注入字段的一部分对于其中一个 fxml 始终为null,而其余注入字段对于另一个为空;这迫使您检查,控制器在多个地方与哪个 fxml 一起使用。确定你为自己节省了一些 loc,但代价是什么? -
另请参阅文档中的 nested controllers 部分,以处理包含的 FXML 文件中的控制器。
-
谢谢大家 - 我周末没去工作。我会尝试这些选项并回复您。