-1

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:

public class ListController {

@FXML
private ListView<Email> 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)
            -> model.setCurrentEmail(newSelection));

    model.currentEmailProperty().addListener((obs, oldEmail, newEmail) -> {
        if (newEmail == null) {
            listView.getSelectionModel().clearSelection();
        } else {
            listView.getSelectionModel().select(newEmail);
        }
    });

    listView.setCellFactory(lv -> new ListCell<Email>() {
        @Override
        public void updateItem(Email mail, boolean empty) {
            super.updateItem(mail, empty);
            if (empty) {
                setText(null);
            } else {
                setText(mail.getMittente());
            }
        }
    });
}

EDIT: @Gnas This is my code now, but it stil doesn't works:

public class ListController {

@FXML
private ListView<Email> 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 -> new ListCell<Email>() {
        @Override
        public void updateItem(Email mail, boolean empty) {
            super.updateItem(mail, empty);
            if (empty) {
                setText(null);
            } else {
                setText(mail.getMittente());
            }
        }
    });
}

EDIT2:

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();
}
Samoa Joe
  • 33
  • 1
  • 8

3 Answers3

4

You're alreading using listView.getSelectionModel().select(newEmail) which is the same method you would use to do what you want. Just call this after you initialize the item list and replace newEmail by the index of the first item, which is 0.

listView.setItems(model.getEmailList());

// assuming the list is never null
if (!listView.getItems().isEmpty()) {
    listView.getSelectionModel().select(0);
}
Gnas
  • 698
  • 1
  • 6
  • 14
0

Try this

listView.getSelectionModel().selectFirst();
Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46
S. Sami
  • 13
  • 1
  • 6
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 04 '22 at 13:22
-1

You can use getSelectionModel().select(0) But you have to make sure you use it right after you've added the elements to the list.

sasieightynine
  • 434
  • 1
  • 5
  • 16
  • nothing new to the other answer, is there? And the OP reported, that it doesn't work in his context. No way to know what's going wrong without him providing a [mcve] but also no reason to duplicate the essence of an another answer ... – kleopatra Jul 27 '19 at 09:24