ListView元素无法用鼠标单击

时间:2018-11-12 16:07:04

标签: java listview javafx fxml

我制作了一个ListView,但是当我尝试用鼠标从中选择一个项目时,它不起作用。选择项目的唯一方法是使用键盘上的箭头。这是我的fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mailbox.ListController">
<ListView fx:id="listView" layoutY="29.0" mouseTransparent="false" prefHeight="371.0" prefWidth="240.0" /></AnchorPane>

这是列表的控制器:

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());
            }
        }
    });
}

这是我声明的地方:

public void start(Stage stage) throws Exception {
    FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
    FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
    FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
    FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("menubar.fxml"));

    AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load(), menuLoader.load());

    ListController listController = listLoader.getController();
    TextAreaController textareaController = textareaLoader.getController();
    TextFieldController fieldController = fieldLoader.getController();
    MenuBarController menuController = menuLoader.getController();

0 个答案:

没有答案