繁体   English   中英

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。

提前致谢 !

您可以对代码进行的最简单的更改是让您的单例类保留一个ObservableList而不是List 然后,您可以直接在表视图中使用它,并直接在其他控制器中对其进行更新。 由于表视图遵循其后备列表,因此对可观察列表的更改将立即反映在表中。

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

    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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