【问题标题】:Not able to populate table view on JAVA FX无法在 JAVAFX 中填充表格视图
【发布时间】:2017-05-24 14:55:42
【问题描述】:

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

ProductOverviewControllerCartOverviewController 发送ObservableList(通过User 对象),该对象将显示在CartOverviewController 的表格中。

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

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

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


}

谁能帮帮我?

【问题讨论】:

    标签: java javafx javafx-2 javafx-8


    【解决方案1】:

    您在initialize() 方法中调用productTable.setItems(cart),该方法在您调用cart = user.getCartList()setUser() 方法之前执行。您要么需要重新设置表格项:

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

    【讨论】:

    • 完成,对不起,我也是 Stack Overflow 的新手.. :)
    猜你喜欢
    • 2017-11-10
    • 2016-10-15
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    相关资源
    最近更新 更多