无法在 JAVA FX 上填充 table 视图

Not able to populate table view on JAVA FX

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

ProductOverviewControllerObservableList(通过 User 对象)发送到 CartOverviewController,将显示到 CartOverviewController 的 table .

问题是:CartOverviewController 似乎收到了 Observablelist(我试图打印包含在可观察列表中的对象名称,结果很好),但是当我 运行程序 table 为空。

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

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");
}


}

谁能帮帮我?

您在 initialize() 方法中调用了 productTable.setItems(cart),该方法在 您调用 [=15= 的 setUser() 方法之前执行].您要么需要再次设置 table 项:

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());
}