Java FX: 如何重写不使用匿名内部类的 RowFactory – 兼容Java 8

huangapple go评论70阅读模式
英文:

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&#39;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 -&gt; new TableRow&lt;&gt;() {
        @Override
        protected void updateItem(Map item, boolean empty) {
            super.updateItem(item, empty);
            ObservableList&lt;String&gt; styles = getStyleClass();
            if (item == null ) {
                setStyle(&quot;&quot;);
            } else {
                final String currentPlaylistRow = (String)item.get(PlaylistBreakdownEnum.PLAYLIST.getColName()) ;
                final String cleanPlaylistString = currentPlaylistRow.replaceAll(&quot;\\(.*\\)&quot;, &quot;&quot;).stripTrailing();
                if (PlaylistEnum.getEnumByPlaylist(cleanPlaylistString).getRetired()) {
                    getStyleClass().add(&quot;retired-row&quot;);
                }
                else {
                    getStyleClass().removeAll(&quot;retired-row&quot;);
                    }
            }
        }
    });

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 -&gt; new TableRow&lt;&gt;() {
    // 自定义 TableRow 实现
});

你需要使用:

playlistDetailsTable.setTableRow(tv -&gt; new TableRow&lt;YOUR_TYPE_HERE&gt;() {
    // 自定义 TableRow 实现
});

在这里,将YOUR_TYPE_HERE替换为与你的TableView使用的类型。换句话说,如果你的playlistDetailsTable是一个TableView&lt;Playlist&gt;,你将使用TableRow&lt;Playlist&gt;

英文:

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 -&gt; new TableRow&lt;&gt;() {
    // custom TableRow implementation
});

You have to use:

playlistDetailsTable.setTableRow(tv -&gt; new TableRow&lt;YOUR_TYPE_HERE&gt;() {
    // 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&lt;Playlist&gt; then you would use TableRow&lt;Playlist&gt;.

huangapple
  • 本文由 发表于 2020年9月12日 11:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63856629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定