1

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?

  • [You can't use the diamond operator with anonymous classes in Java 7 or 8](https://stackoverflow.com/questions/22200647/why-cant-java-7-diamond-operator-be-used-with-anonymous-classes). Make the type explicit (e.g. `new TableRow() { ... }`). – Slaw Sep 12 '20 at 05:24
  • See if https://stackoverflow.com/questions/48979109/javafx-using-setrowfactory-to-highlight-new-rows helps. – SedJ601 Sep 12 '20 at 05:25

1 Answers1

1

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>.

Slaw
  • 37,820
  • 8
  • 53
  • 80