title: Autoselect first Item of a ListView in JavaFX
body: <p>I created a ListView, but when I open it, no elements are selected. How can I autoselect the first item that get loaded? I'm asking this because I'm trying to create an email client and I need that when the user opens the program the informations of the first email must be automatically loaded. This is my code: </p>
  
  <pre><code>public class ListController {
  
  @FXML
  private ListView&lt;Email&gt; listView;
  private DataModel model;
  
  public void initModel(DataModel model) {
      if (this.model != null) {
          throw new IllegalStateException("Model can only be initialized once");
      }
      this.model = model;
      model.loadData(null);
      listView.setItems(model.getEmailList());
      listView.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection)
              -&gt; model.setCurrentEmail(newSelection));
  
      model.currentEmailProperty().addListener((obs, oldEmail, newEmail) -&gt; {
          if (newEmail == null) {
              listView.getSelectionModel().clearSelection();
          } else {
              listView.getSelectionModel().select(newEmail);
          }
      });
  
      listView.setCellFactory(lv -&gt; new ListCell&lt;Email&gt;() {
          @Override
          public void updateItem(Email mail, boolean empty) {
              super.updateItem(mail, empty);
              if (empty) {
                  setText(null);
              } else {
                  setText(mail.getMittente());
              }
          }
      });
  }
  </code></pre>
  
  <p>EDIT: @Gnas This is my code now, but it stil doesn't works:</p>
  
  <pre><code>public class ListController {
  
  @FXML
  private ListView&lt;Email&gt; listView;
  private DataModel model;
  
  public void initModel(DataModel model) {
      if (this.model != null) {
          throw new IllegalStateException("Model can only be initialized once");
      }
      this.model = model;
      model.loadData(null);
      listView.setItems(model.getEmailList());
      if (!listView.getItems().isEmpty()) {
          listView.getSelectionModel().select(0);
      }
  
      listView.setCellFactory(lv -&gt; new ListCell&lt;Email&gt;() {
          @Override
          public void updateItem(Email mail, boolean empty) {
              super.updateItem(mail, empty);
              if (empty) {
                  setText(null);
              } else {
                  setText(mail.getMittente());
              }
          }
      });
  }
  </code></pre>
  
  <p>EDIT2:</p>
  
  <pre><code>public void start(Stage stage) throws Exception {
      FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
      FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("menubar.fxml"));
      FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
      FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
      FXMLLoader buttonLoader = new FXMLLoader(getClass().getResource("button.fxml"));
  
      AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load(), menuLoader.load(), buttonLoader.load());
  
      ListController listController = listLoader.getController();
      MenuBarController menuController = menuLoader.getController();
      TextAreaController textareaController = textareaLoader.getController();
      TextFieldController fieldController = fieldLoader.getController();
      ButtonController buttonController = buttonLoader.getController();
  
      DataModel model = new DataModel();
      listController.initModel(model);
      menuController.initModel(model);
      textareaController.initModel(model);
      fieldController.initModel(model);
      buttonController.initModel(model);
  
      Scene scene = new Scene(root, 603, 403);
      stage.setScene(scene);
      stage.show();
  }
  </code></pre>