I encountered a compiler warning in this common situation of adding columns to a table view control in a JavaFX app.
To demonstrate, here is my modified version of the code example seen in the Javadoc for the JavaFX/OpenJFX class TableView
. Note the getColumns
line.
public class HelloApplication extends Application
{
@Override
public void start ( Stage stage )
{
TableView < Person > tableViewPersons = new TableView <> ( );
ObservableList < Person > persons = FXCollections.observableArrayList ( this.fetchPersons ( ) );
tableViewPersons.setItems ( persons );
TableColumn < Person, String > firstNameCol = new TableColumn <> ( "First Name" );
firstNameCol.setCellValueFactory ( new PropertyValueFactory <> ( persons.get ( 0 ).firstNameProperty ( ).getName ( ) ) );
TableColumn < Person, String > lastNameCol = new TableColumn <> ( "Last Name" );
lastNameCol.setCellValueFactory ( new PropertyValueFactory <> ( persons.get ( 0 ).lastNameProperty ( ).getName ( ) ) );
tableViewPersons.getColumns ( ).setAll ( firstNameCol , lastNameCol ); // <---- Warning: Unchecked generics array creation for varargs parameter.
Scene scene = new Scene ( tableViewPersons , 320 , 240 );
stage.setTitle ( "JavaFX Example" );
stage.setScene ( scene );
stage.show ( );
}
…
The line:
tableViewPersons.getColumns ( ).setAll ( firstNameCol , lastNameCol );
… generates a warning for:
Warning: Unchecked generics array creation for varargs parameter
I understand from this Answer on Java unchecked: unchecked generic array creation for varargs parameter, and from this post, that the issue involves ambiguity about the type of the arguments being passed.
If my goal is to resolve this compiler warning, then one solution is to make explicit the parameterized type of the collection of TableColumn
objects be passed to ObservableList#setAll
. So we can explicitly declare a List
of the TableColumn
objects.
List < TableColumn < Person, ? > > columns = List.of ( firstNameCol , lastNameCol ); // <--- Adding this line to make explicit the parameterized type of `TableColumn` to resolve the "Unchecked generics" warning.
tableViewPersons.getColumns ( ).setAll ( columns );
See that new code in the full context:
public class HelloApplication extends Application
{
@Override
public void start ( Stage stage )
{
TableView < Person > tableViewPersons = new TableView <> ( );
ObservableList < Person > persons = FXCollections.observableArrayList ( this.fetchPersons ( ) );
tableViewPersons.setItems ( persons );
TableColumn < Person, String > firstNameCol = new TableColumn <> ( "First Name" );
firstNameCol.setCellValueFactory ( new PropertyValueFactory <> ( persons.get ( 0 ).firstNameProperty ( ).getName ( ) ) );
TableColumn < Person, String > lastNameCol = new TableColumn <> ( "Last Name" );
lastNameCol.setCellValueFactory ( new PropertyValueFactory <> ( persons.get ( 0 ).lastNameProperty ( ).getName ( ) ) );
List < TableColumn < Person, ? > > columns = List.of ( firstNameCol , lastNameCol ); // <--- Adding this line to make explicit the parameterized type of `TableColumn` to resolve the "Unchecked generics" warning.
tableViewPersons.getColumns ( ).setAll ( columns );
Scene scene = new Scene ( tableViewPersons , 320 , 240 );
stage.setTitle ( "JavaFX Example" );
stage.setScene ( scene );
stage.show ( );
}
…
My question:
- Is this a valid, complete solution to resolving the the "Unchecked generics" warning?
- Is there any other simpler way to resolve the "Unchecked generics" warning? (I would rather not suppress the warning.)