JavaFX - 场景 - 在另一个场景中修改变量

JavaFX - Scene - Modify variable in another scene

我有一个可以创建数据的场景,现在我想将它放入我的 TableView "TV_currency"。

但是这个进入了另一个已经打开的场景,我不想关闭并重新打开这个。

你能看一下吗?

ControllerOptions.java(TV_currency在这里):

public class ControllerOptions implements Initializable{
//VARIABLES    
@FXML private   TableView<Currency> TV_currency;
@FXML private   TableColumn<Currency, String> TC_name;
@FXML private   TableColumn<Currency, Double> TC_value;

private ObservableList<Currency> currencies = FXCollections.observableArrayList();

//FUNCTIONS
@Override
public void initialize(URL location, ResourceBundle rb){
    //initialisation Table Currencies
    for (Currency currency : Datas.getInstance().getCurrencies()) {
        currencies.add(currency);
    }       
    TC_name.setCellValueFactory(new PropertyValueFactory<Currency, String>("name"));
    TC_value.setCellValueFactory(new PropertyValueFactory<Currency, Double>("value"));
    TV_currency.setItems(currencies); //TODO Corriger setItems() de la TableView
}

@FXML void add_currency(MouseEvent event) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("options/currency_add.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.initModality(Modality.APPLICATION_MODAL);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setTitle("Add new currency");
    stage.setScene(new Scene(root1));  
    stage.setFullScreen(true);
    stage.show();
}

@FXML void open_options(ActionEvent event) throws IOException {
    Group actor = new Group();
    actor.getChildren().add(FXMLLoader.load(getClass().getResource("options.fxml")));
    com.courieux.wheresmymoney.Main.setScene(actor, "Where's My Money");
}
}

ControllerCurrencyAdd.java(我要编辑的地方TV_currency):

public class ControllerCurrencyAdd {

@FXML private TextField TF_name;
@FXML private TextField TF_value;
@FXML private TextField TF_acronym;

@FXML
void add(ActionEvent event) {
    Currency currency = new Currency(TF_name.getText(), Double.valueOf(TF_value.getText()));
    Datas.getInstance().addCurrency(currency);

    //==> HERE I NEED TO EDIT VARIABLES BELOW <==
    //currencies.setAll(Datas.getInstance().getCurrencies());
    //TV_currency.setItems(currencies);
}
}

Datas.java :

public class Datas {

//SINGLETON PATTERN
private Datas() {}
private static Datas INSTANCE = new Datas();    
public static Datas getInstance(){
    return INSTANCE;
}
//END SINGLETON PATTERN

//VARS
private List<Currency> currencies = new ArrayList<>();

//FUNCTIONS - Add/Edit/Delete
public void addCurrency(Currency currency){
    currencies.add(currency);
}

//FUNCTIONS - getter/setters
public List<Currency> getCurrencies() {
    return currencies;
}
public void setCurrencies(List<Currency> currencies) {
    this.currencies = currencies;
}
}

Currency.java :

public class Currency {

//VARS
private     String      name;
private     double      value;

//FUNCTIONS - constructors
public Currency(String name, double value) {
    setName(name);
    setValue(value);
}

//FUNCTIONS
public boolean equals(Currency currency){
    if(this.name == currency.getName()
            && this.value == currency.getValue()){
        return true;
    } else {
        return false;
    }
}

//FUNCTIONS - getters/setters
public void setName(String name) {
    this.name = name;
}
public String getName() {
    return name;
}
public void setValue(double value) {
    this.value = value;
}
public double getValue() {
    return value;
}
}

然后我想将新数据放入 ObservableList "currencies" 并用它设置 TV_currency。

提前致谢!

您可以对代码进行的最简单更改是让您的单例 class 持有 ObservableList 而不是 List。然后你可以直接在 table 视图中使用它,并直接在其他控制器中更新它。由于 table 视图观察其支持列表,因此对可观察列表的更改将立即反映在 table.

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Datas {

    // SINGLETON PATTERN
    private Datas() {
    }

    private static Datas INSTANCE = new Datas();

    public static Datas getInstance() {
        return INSTANCE;
    }
    // END SINGLETON PATTERN

    // VARS
    private ObservableList<Currency> currencies = FXCollections.observableArrayList();

    // FUNCTIONS - Add/Edit/Delete
    public void addCurrency(Currency currency) {
        currencies.add(currency);
    }

    // FUNCTIONS - getter/setters
    public ObservableList<Currency> getCurrencies() {
        return currencies;
    }

}

然后

import java.io.IOException;
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.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class ControllerOptions implements Initializable {
    // VARIABLES
    @FXML
    private TableView<Currency> TV_currency;
    @FXML
    private TableColumn<Currency, String> TC_name;
    @FXML
    private TableColumn<Currency, Double> TC_value;

    private ObservableList<Currency> currencies = FXCollections.observableArrayList();

    // FUNCTIONS
    @Override
    public void initialize(URL location, ResourceBundle rb) {
        // initialisation Table Currencies

        TC_name.setCellValueFactory(new PropertyValueFactory<Currency, String>("name"));
        TC_value.setCellValueFactory(new PropertyValueFactory<Currency, Double>("value"));

        // Note how the list from Datas is used directly in the table:
        TV_currency.setItems(Datas.getInstance().getCurrencies()); 
    }

    @FXML
    void add_currency(MouseEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("options/currency_add.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle("Add new currency");
        stage.setScene(new Scene(root1));
        stage.setFullScreen(true);
        stage.show();
    }

    @FXML
    void open_options(ActionEvent event) throws IOException {
        Group actor = new Group();
        actor.getChildren().add(FXMLLoader.load(getClass().getResource("options.fxml")));
        Main.setScene(actor, "Where's My Money");
    }
}

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class ControllerCurrencyAdd {

    @FXML
    private TextField TF_name;
    @FXML
    private TextField TF_value;
    @FXML
    private TextField TF_acronym;

    @FXML
    void add(ActionEvent event) {
        Currency currency = new Currency(TF_name.getText(), Double.valueOf(TF_value.getText()));

        // since we update the list used as the table's backing list, the table will automatically update:
        Datas.getInstance().addCurrency(currency);

    }
}

您的 Datas class 在 MVC(及相关)设计模式中被认为是 "model"。通常,将其设为单例可能会限制您稍后修改应用程序(许多程序员认为这是一种反模式)。您可能会考虑将其设为常规 class 并使用依赖注入技术让每个控制器访问同一实例。