我有一个TableView,其中有一堆已分配给它的数据。在此之后,我想遍历所有行,检查一些条件,然后根据该条件使用CSS为每个条件绘制。为什么以下产生IndexOutOfBoundsException?相反,为什么我不检索我想要的行?为什么我创建的数组是空的?无法从数据分配后从TableView中检索所有行JavaFX
int i = 0;
for (Node n: tableViewPriority.lookupAll("TableRow")) {
System.out.println(n);
if (n instanceof TableRow) {
TableRow row = (TableRow) n;
System.out.println(row);
if (tableViewPriority.getItems().get(i).getPriority().equals("Low")) {
row.getStyleClass().add("tableViewGreen");
}
i++;
if (i == tableViewPriority.getItems().size())
break;
}
}
例外:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
at app.controller.TableViewController.populateTable(TableViewController.java:356)
at app.controller.TableViewController.organizeTable(TableViewController.java:318)
at app.controller.TableViewController.assignTicketsToTable(TableViewController.java:236)
at app.controller.TableViewController.lambda$null$0(TableViewController.java:202)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
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$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
例外(我假设(tableViewPriority.getItems()。get(i)...)')表示'tableViewPriority.getItems()'是空的。但正如答案中所指出的那样,这种方法根本不起作用(即使您先填充表格):一旦开始滚动表格视图,没有任何行将具有正确的样式。使用'rowFactory'。 –
是的,我尝试过使用RowFactory,但我无法检索行。我需要包含数据的行来应用条件。 – santafebound
你不能“检索行”。答案就是这一点。您需要创建行,以便在调用updateItem()方法时更新其样式。您应该显示您使用行工厂进行的尝试。 –