2017-05-24 4 views
2

私はJavaFXの新機能です。開発しようとしているアプリケーションには、ProductOverviewControllerCartOverviewControllerの2つのコントローラがあります。Java FXのテーブルビューにデータを取り込めません

ProductOverviewControllerCartOverviewControllerの表に表示されることになるCartOverviewControllerObservableList(トラフUserオブジェクト)に送信します。

CartOverviewControllerObservablelist(私はオブザーバブルリストに含まれているオブジェクトの名前を良好な結果で印刷しようとしました)を受け取っているようですが、プログラムを実行するとテーブルが空です。

私はすでにこの問題についてこれまでのすべての議論を読みましたが、私はこの問題を解決できませんでした。 これはコードです:

1)ProductOverviewController

public class ProductOverviewController { 


public static User user; 
public static Product product; 
private MainApp mainApp; 

//FXML tag is used to link view controller to the view 
@FXML 
private TableView<Product> productTable; 

@FXML 
private TableColumn<Product, String> productNameColumn; 
@FXML 
private TableColumn<Product, Double> productPriceColumn; 

@FXML 
private Label idLabel; 
@FXML 
private Label productNameLabel; 
@FXML 
private Label productPriceLabel; 
@FXML 
private Label productInfoLabel; 
@FXML 
private Label userLabel; 

/* 
* Reference to the main app 
*/ 




//constructor 
public ProductOverviewController(){ 
} 


//initializer. automatically called after fxml has been loaded 
@FXML 
private void initialize() throws SQLException, IOException{ 

    //Initialize Product table with 2 column 
    productNameColumn.setCellValueFactory(cellData -> cellData.getValue().productNameProperty()); 
    productPriceColumn.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject()); 

    /*THIS CODE IS FOR SYNCRONIZING 
    * 
    */ 

    //Clear person details 

    showProductDetails(null); 

    //Listen for changes and show details 
    productTable.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue)-> { 
     try { 
      showProductDetails(newValue); 
      product = newValue; 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    }); 

} 

//is called by the main app to give a reference back to itself 

public void setMainApp(MainApp mainApp){ 

    this.mainApp = mainApp; 
    //add observable list to the table 
    productTable.setItems(mainApp.getProductData()); 
} 

//fill all text field with products'data in the right table 

private void showProductDetails(Product product) throws SQLException, IOException{ 

    if(product != null){ 
     //fill table's labels 
     idLabel.setText(Integer.toString(product.getIdProduct())); 
     productNameLabel.setText(product.getProductName()); 
     productPriceLabel.setText(Double.toString(product.getProductPrice())); 
     productInfoLabel.setText(product.getProductInfo()); 
     if(user != null && user.isLogin() == true){ 
      userLabel.setText(user.getUsername()); 
     }else if(user.isLogin() == false){ 
      userLabel.setText("Guest"); 
     } 

    }else{ 

     idLabel.setText(""); 
     productNameLabel.setText(""); 
     productPriceLabel.setText(""); 
     productInfoLabel.setText(""); 


    } 
} 

@FXML 
public void handleViewChart() throws IOException, SQLException{ 

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CartOverview.fxml")); 
    Parent root; 
    try{ 

     root = fxmlLoader.load(); 
     Scene scene = new Scene(root); 
     Stage stage = new Stage(); 
     stage.setScene(scene); 
     ((CartOverviewController)fxmlLoader.getController()).setUser(user); 
     stage.show(); 
    }catch(IOException e){ 

     e.printStackTrace(); 
    } 



} 

@FXML 
private void handleAddToCart(){ 
    //send to CartOverviewControler user with the dispaly list and print for debug 
    user.addToCart(product); 
    System.out.println(user.getCartList().get(0).getProductName()); 

} 

} 

2)CartOverviewController

public class CartOverviewController implements Initializable { 

public User user; 

private ObservableList<Product> cart = FXCollections.observableArrayList(); 

@FXML 
private TableView<Product> productTable; 

@FXML 
private TableColumn<Product, String> productNameColumn; 
@FXML 
private TableColumn<Product, Double> productPriceColumn; 
@FXML 
private TableColumn<Product, Double> productAmountColumn; 




public void setUser(User user) throws SQLException, IOException{ 

    this.user = user; 
    this.cart = this.user.getCartList(); 
    System.out.println(this.cart.get(0).getProductName()); 
} 


@Override 
public void initialize(URL location, ResourceBundle resources) { 
    // TODO Auto-generated method stub 

    //Method 1 
    /* 
    productNameColumn.setCellValueFactory(new PropertyValueFactory<Product, String> ("productName")); 
    productPriceColumn.setCellValueFactory(new PropertyValueFactory<Product, Double> ("productPrice")); 
    productAmountColumn.setCellValueFactory(new PropertyValueFactory<Product, Double> ("productAmount")); 
    */ 

    //Method 2 

    productNameColumn.setCellValueFactory(cellData -> cellData.getValue().productNameProperty()); 
    productPriceColumn.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject()); 
    productAmountColumn.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject()); 


    //added this for debug 
    if(cart != null && productTable != null) 
     productTable.setItems(cart); 
    else if(cart == null) 
     System.out.println("cart is null"); 
    else if(productTable == null) 
     System.out.println("table is null"); 
    else 
     System.out.println("else is null"); 
} 


} 

誰も私を助けることができますか?

答えて

2

あなたはcart = user.getCartList()を呼んでいるsetUser()方法を実行しているinitialize()方法、でproductTable.setItems(cart)を呼び出します。あなたのいずれかを再度テーブルの項目を設定する必要があります。

public void setUser(User user) throws SQLException, IOException{ 

    this.user = user; 
    this.cart = this.user.getCartList(); 
    productTable.setItems(cart); 
} 

か、単に(代わりにそれを置き換えるの)現在のcartリストの内容を更新します。

public void setUser(User user) throws SQLException, IOException{ 

    this.user = user; 
    this.cart.setAll(this.user.getCartList()); 
} 
+0

ああありがとう!今それは動作します:D – Knospfer

+0

完了、申し訳ありません私もスタックオーバーフローで新しいです:) – Knospfer

関連する問題