无法在JAVA FX上填充表格视图

时间:2017-05-24 14:55:42

标签: java javafx javafx-2 javafx-8

我是JavaFX的新手,在我尝试开发的应用程序中,有两个控制器:ProductOverviewControllerCartOverviewController

ProductOverviewControllerCartOverviewController ObservableList发送User对象,该CartOverviewController对象将显示在CartOverviewController表中。

问题是:Observablelist似乎收到了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()); } } (我试图打印一个包含在可观察列表中的对象名称并且效果很好)但是当我运行程序时桌子是空的。

我已经阅读了之前关于此问题的所有讨论,但我无法解决此问题。 这是代码:

1)ProductOverviewController

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)CartOverviewController

{{1}}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

您在productTable.setItems(cart)方法中调用initialize(),该方法在您调用setUser()的{​​{1}}方法之前执行。您需要再次设置表项:

cart = user.getCartList()

或者只是更新当前public void setUser(User user) throws SQLException, IOException{ this.user = user; this.cart = this.user.getCartList(); productTable.setItems(cart); } 列表的内容(而不是替换它):

cart