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

时间:2017-08-02 21:27:16

标签: java javafx tableview scenebuilder 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“currency”并用它设置TV_currency。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以对代码进行的最简单的更改是让您的单例类包含for(i=0;i<t;i++) scanf("%s",cases[i]); 而不是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(及相关)设计模式中的“模型”。通常,将其设置为单例可能会限制您稍后修改应用程序(许多程序员认为这是一种反模式)。您可以考虑将此作为常规类,并使用依赖注入技术为每个控制器提供对同一实例的访问。

相关问题