我创建了一个ListView,但是当我打开它时,没有选择任何元素。如何自动选择要装载的第一件物品?我之所以这样问是因为我正在尝试创建电子邮件客户端,并且我需要在用户打开程序时必须自动加载第一封电子邮件的信息。这是我的代码:
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());
}
}
});
}
编辑:@Gnas现在这是我的代码,但是它仍然无法使用:
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();
}
答案 0 :(得分:4)
您正在使用listView.getSelectionModel().select(newEmail)
,这与您要执行的操作相同。在初始化商品列表并将newEmail
替换为第一商品的索引(即0)之后,只需调用此函数即可。
listView.setItems(model.getEmailList());
// assuming the list is never null
if (!listView.getItems().isEmpty()) {
listView.getSelectionModel().select(0);
}
答案 1 :(得分:-1)
您可以使用getSelectionModel().select(0)
,但是必须确保在将元素添加到列表之后立即使用它。