2017-08-02 20 views
0

私はデータを作成できるシーンを持っています。これをTableViewの "TV_currency"に入れたいと思います。JavaFX - シーン - 別のシーンの変数を変更します。

しかし、これはすでに開いている別のシーンにあります。私はこのシーンを閉じて再オープンしたくありません。

あなたはそれを見ることができますか?

ControllerOptions.java(TV_currencyはこちら):(私は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:

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を設定したいと思います。

ありがとうございます!

+0

[最小限の完全で検証可能な例](https://stackoverflow.com/help/mcve)を作成する必要があります。 – Sedrick

+0

私はそれを完了しようとします –

+0

データを別のクラス(デザインパターンの用語では「モデル」と呼ばれます)に格納し、そのクラスのインスタンスをコントローラと共有します(これは本質的にMVCデザインパターン)。例については、https://stackoverflow.com/questions/32342864/applying-mvc-with-javafxをご覧ください。ここで、シングルトン 'Datas'が' ObservableList'を返すようにすると、あるコントローラで 'TV_currency.setItems(Datas.getInstance()。getCurrencies())'のようなことをするだけで、 'Datas.getInstance()。addCurrency (...) 'はテーブルを自動的に更新するはずです。 –

答えて

0

コードに最も簡単な変更は、シングルトンクラスにListの代わりにObservableListを保持させることです。次に、それをテーブルビューで直接使用し、他のコントローラで直接更新することができます。テーブルビューはバッキングリストを観察するので、観察可能リストの変更はすぐにテーブルに反映されます。

I.e.

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クラスは、MVC(および関連)デザインパターンの "モデル" であると考えられています。通常これをシングルトンにすることで、後でアプリケーションを変更するという点で少し制限されることがあります(多くのプログラマーはこれをアンチパターンと考えています)。これを通常のクラスにし、依存関係注入技術を使用して各コントローラに同じインスタンスへのアクセスを与えることを検討することもできます。

+0

それは完璧な仕事です! –

関連する問題