首页 > 解决方案 > javafx - 未捕获表视图鼠标事件

问题描述

我的 fxml 中有一个 TableView。在控制器中,该表使用@FXML 注释进行映射。

@FXML
TableView<BankMovement> table;

我还在 fxml 文档中设置了表格元素

onMouseReleased = "#handleRowSelect"

最后总是在控制器中我有以下方法

@FXML
private void handleRowSelect(MouseEvent event){
    Document row = table.getSelectionModel().getSelectedItem();
    if (row == null) return;
    if(row != temp){
        temp = row;
        lastClickTime = new Date();
    } else if(row == temp) {
        Date now = new Date();
        long diff = now.getTime() - lastClickTime.getTime();
        if (diff < 300){ 
            System.out.println("Edit dialog");
            System.out.print(row.getDescription());
        } else {
            lastClickTime = new Date();
            System.out.print(row.getFile());

        }
    }
}

相反,这里是将数据绑定到表的位置

@Override
public void initializeTable() {
    table.setItems(subList());
    colId.setCellValueFactory(new PropertyValueFactory<>("idDocument"));
    colIdCategory.setCellValueFactory(d -> new SimpleStringProperty(d.getValue().getCategory().getName()));
    colIdCategoryChild.setCellValueFactory(d -> new SimpleStringProperty(d.getValue().getCategoryChild().getName()));
    colIdDocument.setCellFactory(getFileCellFactory());
    colIdDocument.setCellValueFactory(new PropertyValueFactory<>("file"));
    colIdDescription.setCellValueFactory(new PropertyValueFactory<>("description"));
    colIdDate.setCellValueFactory(d -> new SimpleStringProperty(Utils.localTimeToItalianDate(d.getValue().getDocumentDate())));
}

即使我单击表格,我也从不输入 handleRowSelect 方法。

我的印象是,当我单击行时,对单元格的单击是受管理的,而不是在行上。好像事件没有传播。

这是.xml

<AnchorPane minWidth="-Infinity"  prefHeight="900.0" prefWidth="1510.0" styleClass="background-white" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
    <children>
        <VBox prefHeight="886.0" prefWidth="1465.0" style="-fx-min-width: 100%;" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="330.0" AnchorPane.topAnchor="10.0">
            <children>
                <TitledPane animated="false" minHeight="-Infinity" minWidth="-Infinity" prefHeight="880.0" prefWidth="1465.0" text="%app.pane.document">
                    <content>
                        <VBox minWidth="-Infinity" prefHeight="815.0" prefWidth="1474.0">
                            <children>
                                <TableView onMouseReleased="#handleRowSelect" fx:id="table" minHeight="-Infinity" minWidth="-Infinity" prefHeight="746.0" prefWidth="1450.0">
                                    .......
                                    <padding>
                                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                                    </padding>
                                </TableView>
                                <Pagination fx:id="pagination" prefHeight="73.0" prefWidth="1450.0" />
                            </children>
                        </VBox>
                    </content>
                </TitledPane>
            </children>
        </VBox>
    </children>
</AnchorPane>

标签: javajavafxmouseevent

解决方案


推荐阅读