【发布时间】:2017-06-16 10:22:00
【问题描述】:
我有一个 TableView,其中已经分配了一堆数据。在此之后,我想遍历所有行,检查一些条件,然后根据该条件用 CSS 绘制每一行。为什么以下会产生 IndexOutOfBoundsException?相反,为什么我不检索我想要的行?为什么我创建的数组是空的?
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)
【问题讨论】:
-
异常(我假设发生在
if (tableViewPriority.getItems().get(i)...)行)表明tableViewPriority.getItems()为空。但是,如答案所示,这种方法根本行不通(即使您先填充表格):一旦开始滚动表格视图,所有行都不会具有正确的样式。使用rowFactory。 -
是的,我尝试过使用 RowFactory,但我在检索行时遇到了麻烦。我需要其中包含数据的行来应用条件。
-
您不能“检索行”。这就是答案中的重点。您需要创建在调用
updateItem()方法时更新其样式的行。您应该展示您使用行工厂所做的尝试。