gpt4 book ai didi

JavaFX observableArrayList 总是被覆盖

转载 作者:行者123 更新时间:2023-12-01 12:04:47 27 4
gpt4 key购买 nike

我是 Java 新手,遇到了一些问题。

有两个类,我尝试从 PopupCarEntryController 类写入 ChoiceCarController 类中的 ArrayList 。使用 addCarToList 方法。

但是每次我启动这个方法时,ArrayList 都是空的或者被覆盖。

选择汽车 Controller :

import Car;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;

public class FXMLChoiceCarController implements Initializable {

private FahrtenbuchFX application;

public FXMLChoiceCarController() {
this.carChoiceBox = new ChoiceBox(carChoiceBoxData);
}

@FXML
private void handleSelectButtonAction(ActionEvent event) {
if (!carChoiceBox.getSelectionModel().isEmpty()) {
application.FahrtenbuchView();
} else {

}
}

@FXML
private void handleAddButtonAction(ActionEvent event) {
application.showPopup();
}

@FXML
private void handleExitButtonAction(ActionEvent event) {
System.exit(0);
}


public ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();

@FXML
private ChoiceBox carChoiceBox;

@FXML
private void handleCarChoiceBoxAction(ActionEvent event) {

}

@Override
public void initialize(URL url, ResourceBundle rb) {
carChoiceBox.getSelectionModel().selectFirst();
carChoiceBox.setItems(carChoiceBoxData);
// this.carChoiceBoxData.add(new Car("asdf", "asdf"));
System.out.println(getCarList());
}

public void addCarToList(Car car) {
System.out.println(getCarList());
carChoiceBoxData.add(car);
System.out.println(getCarList());
}

public String getCarList() {
return carChoiceBoxData.toString();
}

public void setApp(FahrtenbuchFX application) {
this.application = application;
}

}

和 PopupCarEntryController

import Car;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;


public class FXMLPopupCarEntryController implements Initializable {

private FahrtenbuchFX application;

@FXML
private Label labelErrorMessage;

@FXML
private TextField textFieldProducer;

@FXML
private TextField textFieldModel;

@FXML
private TextField textFieldEngine;

@FXML
private TextField textFieldFuel;

@FXML
private TextField textFieldFlag;

@FXML
private void handleSaveButtonAction(ActionEvent event) {
FXMLChoiceCarController carController = new FXMLChoiceCarController();
carController.addCarToList(new Car(textFieldProducer.getText(), textFieldModel.getText()));

}

@FXML
private void handleCloseButtonAction(ActionEvent event) {
application.hidePopup();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
//TODO
}

public void setApp(FahrtenbuchFX application) {
this.application = application;
}
}

我不明白的是,当我在 void initialize 方法中初始化 List 并在 addCarToList 方法中执行 out.println 时,结果是不同的。就像变量指向不同的 ArrayList 时一样。

最佳答案

那是因为每次你实例化 Controller 类时都是这样的:

FXMLChoiceCarController carController = new FXMLChoiceCarController();

所以你每次都会创建列表,例如:

public ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();

如果您想重用该列表,请将列表设为静态,例如:

public static ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();

因此,您的列表只有一个实例。

关于JavaFX observableArrayList 总是被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701531/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com