2017-08-02 125 views
0

我有一個場景,我可以創建一個數據,現在我想把它放到我的TableView「TV_currency」中。JavaFX - 場景 - 在另一個場景中修改變量

但是這個人已經進入另一個已經開放的場景,我不想關閉並重新打開這個場景。

你可以看看它嗎?

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「貨幣」並設置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

您可以對代碼進行的最簡單的更改是讓您的單例類保留ObservableList而不是List。然後,您可以直接在表格視圖中使用它,並直接在其他控制器中更新它。由於表格視圖觀察其支持列表,所以可觀察列表的更改將立即反映在表格中。

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

這是完美的工作! –