如何正确填充treeTableView
How to fill the treeTableView correctly
我在数据库中有一条记录,要获取的记录必须在树 table 视图中。下面是填充方法:
private void updateGoods(){
goodsPane.setCenter(goodTreeTableView);
List<Good> goodAndFoldersList;
try {
goodAndFoldersList = goodsService.getGoods();//database query
}catch (SQLException e){
goodAndFoldersList = new ArrayList<>();
log.log(Level.ERROR, e.getMessage());
}
List<Good> goods = new ArrayList<Good>();//list for roots
List<Good> roots = new ArrayList<Good>();//list for goods themselves
for (Good good : goodAndFoldersList) {
if (good.isIs_folder()) {
roots.add(good);
} else {
goods.add(good);
}
}
TreeItem<Good> rootItem = new TreeItem<>();//the main root
for (Good root : roots) {
Long folderId = root.getId();
TreeItem<Good> rootTreeItem = new TreeItem<>(root);
for (Good good : goods) {//looking for goods for each folder
if (good.getFolderId() == folderId) {
TreeItem<Good> goodTreeItem = new TreeItem<>(good);
rootTreeItem.getChildren().add(goodTreeItem);
}
}
rootItem.getChildren().add(rootTreeItem);//adding every folder to the main root
}
goodTreeTableView = new TreeTableView<>(rootItem);
}
使用此方法后,用户在 treeTableView 中看不到任何内容。也许有必要定义单元格内容,但是这段代码放在 goodTreeTableView = new TreeTableView<>(rootItem);
之前会掉落 NullPointerException:
goodName.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, String> param) ->
new ReadOnlyStringWrapper(param.getValue().getValue().getName()));
folderId.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Number> param) ->
new ReadOnlyLongWrapper(param.getValue().getValue().getFolderId()));
is_folder.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Boolean> param) ->
new ReadOnlyBooleanWrapper(param.getValue().getValue().isIs_folder()));
我想这就是我遇到问题的原因,但为什么会下降 NPE
我想不通。
UPD: 如果我添加设置单元格值工厂,我会得到以下堆栈跟踪:
javafx.fxml.LoadException:
/D:/javaEdi/linkserver/ediagent/target/classes/com/ediagent/edi/gui/Main.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2565)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at com.ediagent.edi.gui.Main.initRootLayout(Main.java:44)
at com.ediagent.edi.gui.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda/1546859350.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda/1712669532.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda/157858792.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda/1225373914.run(Unknown Source)
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$null1(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda/152005629.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2560)
... 18 more
Caused by: java.lang.NullPointerException
at com.ediagent.edi.gui.MainController.updateGoods(MainController.java:309)
at com.ediagent.edi.gui.MainController.initialize(MainController.java:328)
... 28 more
**UPD:**rootItem:
您正在通过重新初始化将数据添加到 FXML 上的 GUI 元素。避免重新初始化 FXML 元素。
您可以将 updateGoods()
的签名更改为:
public TreeItem<Good> updateGoods() {
...
}
并在MainController
中使用它来设置项目
goodTreeTableView.setRoot(updateGoods());
最好,您也可以 return 仅来自 updateGoods()
的数据(列表)而不是 return 树项。在Controller中创建并设置为goodTreeTableView
.
的root
我在数据库中有一条记录,要获取的记录必须在树 table 视图中。下面是填充方法:
private void updateGoods(){
goodsPane.setCenter(goodTreeTableView);
List<Good> goodAndFoldersList;
try {
goodAndFoldersList = goodsService.getGoods();//database query
}catch (SQLException e){
goodAndFoldersList = new ArrayList<>();
log.log(Level.ERROR, e.getMessage());
}
List<Good> goods = new ArrayList<Good>();//list for roots
List<Good> roots = new ArrayList<Good>();//list for goods themselves
for (Good good : goodAndFoldersList) {
if (good.isIs_folder()) {
roots.add(good);
} else {
goods.add(good);
}
}
TreeItem<Good> rootItem = new TreeItem<>();//the main root
for (Good root : roots) {
Long folderId = root.getId();
TreeItem<Good> rootTreeItem = new TreeItem<>(root);
for (Good good : goods) {//looking for goods for each folder
if (good.getFolderId() == folderId) {
TreeItem<Good> goodTreeItem = new TreeItem<>(good);
rootTreeItem.getChildren().add(goodTreeItem);
}
}
rootItem.getChildren().add(rootTreeItem);//adding every folder to the main root
}
goodTreeTableView = new TreeTableView<>(rootItem);
}
使用此方法后,用户在 treeTableView 中看不到任何内容。也许有必要定义单元格内容,但是这段代码放在 goodTreeTableView = new TreeTableView<>(rootItem);
之前会掉落 NullPointerException:
goodName.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, String> param) ->
new ReadOnlyStringWrapper(param.getValue().getValue().getName()));
folderId.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Number> param) ->
new ReadOnlyLongWrapper(param.getValue().getValue().getFolderId()));
is_folder.setCellValueFactory((TreeTableColumn.CellDataFeatures<Good, Boolean> param) ->
new ReadOnlyBooleanWrapper(param.getValue().getValue().isIs_folder()));
我想这就是我遇到问题的原因,但为什么会下降 NPE
我想不通。
UPD: 如果我添加设置单元格值工厂,我会得到以下堆栈跟踪:
javafx.fxml.LoadException:
/D:/javaEdi/linkserver/ediagent/target/classes/com/ediagent/edi/gui/Main.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2565)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at com.ediagent.edi.gui.Main.initRootLayout(Main.java:44)
at com.ediagent.edi.gui.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda/1546859350.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda/1712669532.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda/157858792.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda/1225373914.run(Unknown Source)
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$null1(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda/152005629.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2560)
... 18 more
Caused by: java.lang.NullPointerException
at com.ediagent.edi.gui.MainController.updateGoods(MainController.java:309)
at com.ediagent.edi.gui.MainController.initialize(MainController.java:328)
... 28 more
**UPD:**rootItem:
您正在通过重新初始化将数据添加到 FXML 上的 GUI 元素。避免重新初始化 FXML 元素。
您可以将 updateGoods()
的签名更改为:
public TreeItem<Good> updateGoods() {
...
}
并在MainController
中使用它来设置项目
goodTreeTableView.setRoot(updateGoods());
最好,您也可以 return 仅来自 updateGoods()
的数据(列表)而不是 return 树项。在Controller中创建并设置为goodTreeTableView
.