简体   繁体   中英

Java generics compiler error assigning to JavaFX collection

Consider the following code:

public interface ListDisplayObj {
    public String getListDisplayString();
}

public class BusinessObject implements ListDisplayObj {
    ...
     public String getListDisplayString() {
          return "Example String";
     }
}

public class DBUtilityClass {
    public ObservableList<BusinessObject> getMyBusinessObjects() {
        ObservableList<BusinessObject> toReturn =
            FXCollections.observableArrayList();
        // add some values to list
        return toReturn;
    }
}

public class JavaFXGuiController  implements Initializable {

    @FXML private ListView<ListDisplayObj> listItems;

    @Override
public void initialize(URL url, ResourceBundle rb)
{
        ObservableList<ListDisplayObj> objsForList =
            DBUtilityClass.getMyBusinessObjects();
        listItems.setItems(objsForList);
        ...
    }
}

The assignment in initialize() is generating an "incompatible types" compile error between the return type of the getMyBusinessObjects() function and the objsForList type.

What am I doing wrong?

setItems requires a list of the exact type (ObservableList instead of ObservableList), whereas your list is of an implementation of the interface. Define listItems as a ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM