简体   繁体   中英

JavaFX observableArrayList is always overwritten

I'm new on Java and have a little problem.

There are two classes and I have tried to write into a ArrayList in the ChoiceCarController class from the PopupCarEntryController class. With the addCarToList method.

But every time I start this method, the ArrayList is empty or got overwritten.

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;
    }

}

And the 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;
    }
}

The thing that I not understood is that, when I initialize the List in the void initialize method and do an out.println in the addCarToList method the result is different. Like when the variable points to an different ArrayList.

That's because every time you are instantiating controller class like this:

FXMLChoiceCarController carController = new FXMLChoiceCarController();

So you are creating list every time like:

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

if you want to reuse the list, make your list static like:

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

So you just have one instance of your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM