我是Java新手,有一点问题。
有两个类,我试图从PopupCarEntryController类写入ChoiceCarController类中的 ArrayList 。使用addCarToList方法。
但每次启动此方法时,ArrayList都为空或被覆盖。
ChoiceCarController:
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一样。
答案 0 :(得分:1)
那是因为每次你像这样实例化控制器类:
FXMLChoiceCarController carController = new FXMLChoiceCarController();
所以你每次都在创建列表,如:
public ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();
如果您想重复使用列表,请将列表设为静态,如:
public static ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();
所以你只有一个列表实例。