**关闭。**此题需要debugging details。目前不接受答复。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
6小时前关闭
Improve this question
ListView不更新JavaFx
在下面的代码中,名为tourList的ListView不会更新,即使Observable ListtourNameList有更新的数据。当我启动程序时,初始列表显示DB条目,但是当我添加新条目时,没有进行更新。谁能帮帮忙...
public class TourListView implements TourListObserver {
public TourListViewModel tourListViewModel = new TourListViewModel();
public TourFormViewModel tourFormViewModel = new TourFormViewModel();
public FormInputManager formInputManager = new FormInputManager();
Alert alert = new Alert(Alert.AlertType.ERROR);
Alert successPrompt = new Alert(Alert.AlertType.INFORMATION);
//Form Components
@FXML
public ScrollPane tourForm;
@FXML
public TextField formFrom;
public ChoiceBox<String> formTransportType = new ChoiceBox<>();
public TextField formTo;
@FXML
public TextField formName;
@FXML
public TextField formDescription;
@FXML
public Button formSubmitButton;
@FXML
public ListView<String> tourList = new ListView<>();
public TextArea formRouteInformation;
@Override
public void onTourListUpdated() {
Platform.runLater(this::updateTourList); //thread safe update
}
public void updateTourList() {
//check if I am creating a second List
System.out.println("UPDATE TOUR LIST: TourListView");
ObservableList<String> tourNameList = tourListViewModel.getTourNameList();
System.out.println("tourNameList: " + tourNameList.toString() ); //tourNameList has shows new Data
this.tourList.setItems(tourNameList); //this doesn't show new data
//this.tourList.getItems().setAll(tourNameList);
//this.tourList.refresh();
}
@FXML
public void addTour(ActionEvent event) {
System.out.println("Button clicked: addTour");
//validate Input
FormTour formTour = new FormTour(formName.getText(), formDescription.getText(), formFrom.getText(), formTo.getText(), formTransportType.getValue(), formRouteInformation.getText());
String validationString = formInputManager.validateForm(formTour);
if (Objects.equals(validationString, FormMessages.VALID_FORM.getMessage())) {
//post them to DB
System.out.print("VALID INPUT");
System.out.printf(validationString);
tourFormViewModel.postTour(formTour);
tourListViewModel.updateList();
successPrompt.setTitle("Tour has been added");
successPrompt.setContentText(FormMessages.FORM_ADDED.getMessage());
successPrompt.setHeaderText("");
successPrompt.showAndWait();
} else {
//show error message
alert.setTitle("INVALID INPUT");
alert.setContentText(validationString);
alert.setHeaderText("");
alert.showAndWait();
System.out.printf(validationString);
}
}
public void addTourFormShow(ActionEvent event) {
//make TourList invisible
if (tourList.isVisible()) {
tourList.setVisible(false);
tourForm.setVisible(true);
} else {
tourList.setVisible(true);
tourForm.setVisible(false);
}
System.out.println("Button clicked: startForm");
}
@javafx.fxml.FXML
public void initialize() {
tourListViewModel.registerObserver(this);
//init List
ObservableList<String> tourNameListList = FXCollections.observableArrayList();
tourNameListList = tourListViewModel.getTourNameList();
this.tourList.setItems(tourNameListList);
//init ChoiceBox
String[] choiceBoxChoices = {"public transit", "car", "bike", "foot"};
formTransportType.getItems().addAll(choiceBoxChoices);
}
}
我尽力了
this.tourList.getItems().setAll(tourNameList);
this.tourList.refresh();
但也没有用
根据要求,FXML文件:
<SplitPane dividerPositions="0.10738255033557047"
orientation="VERTICAL"
prefHeight="300.0"
prefWidth="174.0"
AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0"
fx:controller="at.fhtw.app.view.TourListView"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="49.0" prefWidth="172.0"
SplitPane.resizableWithParent="false">
<children>
<Label layoutY="5.0" text="Tours"/>
<Button fx:id="addButton" onAction="#addTourFormShow" layoutX="94.0" layoutY="1.0"
mnemonicParsing="false" text="+" AnchorPane.rightAnchor="51.0"
AnchorPane.topAnchor="0.0"/>
<Button layoutX="121.0" layoutY="1.0" mnemonicParsing="false" text="-" AnchorPane.rightAnchor="27.0"
AnchorPane.topAnchor="0.0"/>
<Button layoutX="145.0" layoutY="1.0" mnemonicParsing="false" text="..." AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<fx:include fx:id="tourForm" source="Tour-Form.fxml" visible="false"/>
<ListView fx:id="tourList" prefHeight="262.0" prefWidth="172.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
</items>
</SplitPane>
2条答案
按热度按时间qoefvg9y1#
这是错误的:
永远不要将@FXML注入字段设置为新值。
fxml加载器已经创建了@FXML字段的示例,并将其链接到加载器返回的对象树中。
如果将字段指定为新值,则新值将不会出现在添加到场景图形的加载对象树中,因此永远不会看到新值。只会看到加载器创建的空ListView。
ktecyv1j2#
我解决了这个问题:我对两个fxml组件使用了一个View(ListView用于tourForm和tourList,这意味着我有两个不同的ListView。