英文:
Java FX: How to rewrite a RowFactory without an anonymous inner class - Java 8 Compatible
问题
目前我正在设置项目以使用Java 8。由于限制,这是我能运行的当前最高版本的Java。在从之前的Java 11配置转换为Java时,我遇到了以下问题:
```java
playlistDetailsTable.setRowFactory(tv -> new TableRow<>() {
^
原因:在-source 8中不支持带有匿名内部类的'<>'
(使用-source 9或更高版本启用带有匿名内部类的'<>'')
其中T是类型变量:
T extends Object 声明在类TableRow中
以下是当前代码块,它在此处失败。这只是在播放列表被下架时突出显示表格行。
playlistDetailsTable.setRowFactory(tv -> new TableRow<>() {
@Override
protected void updateItem(Map item, boolean empty) {
super.updateItem(item, empty);
ObservableList<String> styles = getStyleClass();
if (item == null ) {
setStyle("");
} else {
final String currentPlaylistRow = (String)item.get(PlaylistBreakdownEnum.PLAYLIST.getColName()) ;
final String cleanPlaylistString = currentPlaylistRow.replaceAll("\\(.*\\)", "").stripTrailing();
if (PlaylistEnum.getEnumByPlaylist(cleanPlaylistString).getRetired()) {
getStyleClass().add("retired-row");
}
else {
getStyleClass().removeAll("retired-row");
}
}
}
});
是否有人知道如何有效解决这个问题?
<details>
<summary>英文:</summary>
I'm currently getting my project set up to use Java 8. Due to restrictions this is the current highest version of Java I can run. When converting to Java down from my previous Java 11 configuration I ran into the following issue:
playlistDetailsTable.setRowFactory(tv -> new TableRow<>() {
^
reason: '<>' with anonymous inner classes is not supported in -source 8
(use -source 9 or higher to enable '<>' with anonymous inner classes)
where T is a type-variable:
T extends Object declared in class TableRow
Here is the current block of code that it is failing on. This merely higlights a table row iff a playlist has been Retired.
playlistDetailsTable.setRowFactory(tv -> new TableRow<>() {
@Override
protected void updateItem(Map item, boolean empty) {
super.updateItem(item, empty);
ObservableList<String> styles = getStyleClass();
if (item == null ) {
setStyle("");
} else {
final String currentPlaylistRow = (String)item.get(PlaylistBreakdownEnum.PLAYLIST.getColName()) ;
final String cleanPlaylistString = currentPlaylistRow.replaceAll("\\(.*\\)", "").stripTrailing();
if (PlaylistEnum.getEnumByPlaylist(cleanPlaylistString).getRetired()) {
getStyleClass().add("retired-row");
}
else {
getStyleClass().removeAll("retired-row");
}
}
}
});
Does anyone know how to effective go about fixing this?
</details>
# 答案1
**得分**: 1
你的问题并不是由使用匿名类引起的。问题在于,由于某些原因,在Java 7或8中无法在匿名类中使用菱形操作符(diamond operator)。这个特性直到Java 9才被添加。因为你在使用Java 8,你需要更新你的代码以明确指定泛型类型。
举个例子,不要使用:
```java
playlistDetailsTable.setTableRow(tv -> new TableRow<>() {
// 自定义 TableRow 实现
});
你需要使用:
playlistDetailsTable.setTableRow(tv -> new TableRow<YOUR_TYPE_HERE>() {
// 自定义 TableRow 实现
});
在这里,将YOUR_TYPE_HERE
替换为与你的TableView
使用的类型。换句话说,如果你的playlistDetailsTable
是一个TableView<Playlist>
,你将使用TableRow<Playlist>
。
英文:
Your problem is not caused by the use of an anonymous class. The issue is that, due to reasons, you cannot use the diamond operator with anonymous classes in Java 7 or 8. That feature wasn't added until Java 9. Since you're using Java 8 you'll have to update your code to make the generic type explicit.
For example, instead of using:
playlistDetailsTable.setTableRow(tv -> new TableRow<>() {
// custom TableRow implementation
});
You have to use:
playlistDetailsTable.setTableRow(tv -> new TableRow<YOUR_TYPE_HERE>() {
// custom TableRow implementation
});
Where you replace YOUR_TYPE_HERE
with the type used with your TableView
. In other words, if your playlistDetailsTable
is a TableView<Playlist>
then you would use TableRow<Playlist>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论