我想从 JavaFX TableView 中获取选定的行。奇怪的是,当我单击一行一次时,事件会被触发,但不会触发第二次或第三次。为什么?是否有其他方法处理此事件(例如带有 @FXML 注释的普通事件处理程序)?
public PersonController {
private ObservableList<Person> personData = FXCollections.observableArrayList();
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;
@FXML
private void initialize() {
personTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (personTable.getSelectionModel().getSelectedItem() != null) {
showPersonDetails(newValue);
}
});
}
}
FXML
<TableView fx:id="personTable" layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="Vorname" />
<TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Nachname" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
最佳答案
找到了解决方案。我必须初始化 JPA 域类中的属性。
所以代替:
private IntegerProperty id;
你必须写:
private IntegerProperty id = new SimpleIntegerProperty();
关于JavaFX TableView 事件只触发一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25280693/