I've a scene where I can create a data, now I want to put it into my TableView "TV_currency".
But this one is into another scene who's already open and I don't want to close and reopen this one.
Can you take a look to it ?
ControllerOptions.java (TV_currency is here) :
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 (where I want to edit 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;
}
}
Then I want to put new data into the ObservableList "currencies" and set TV_currency with it.
Thanks in advance !