Search code examples
mysqljavafxfxml

I am not able to populate Javafx TableView using fxml


Nothing seems to wrong with the code but the table is not populating.Same code is working well with other place.

public class Test implements Initializable {
@FXML
TableView sort_view;
@FXML
TableColumn sort_id, sort_pname, sort_price, sort_quantity, sort_total;
@Override
public void initialize(URL location, ResourceBundle resources) {
    final ObservableList<Sort> data =
            FXCollections.observableArrayList();
    Sort sort;
    SalesDb db = new SalesDb();
    ResultSet rs = db.sortProduct();

    try {
        while (rs.next()) {
            int id = rs.getInt("ID");
            String productName = rs.getString("ProductName");
            String productPrice = rs.getString("ProductPrice");
            String quantity = rs.getString("Quantity");
            String total = rs.getString("Total");

            sort = new Sort(Integer.toString(id),productName,productPrice,quantity,total);
            data.add(sort);
            System.out.println(sort.getId()+","+sort.getProductName()+","+sort.getPrice()+","+sort.getQuantity()+","+sort.getTotal());
        }
    }catch(Exception  e){
        System.out.println("There is an Exception.");
        System.out.println(e.getMessage());
    }
    sort_id.setCellValueFactory(new PropertyValueFactory<Sort, String>("id"));
    sort_pname.setCellValueFactory(new PropertyValueFactory<Sort, String>("productName"));
    sort_price.setCellValueFactory(new PropertyValueFactory<Sort, String>("price"));
    sort_quantity.setCellValueFactory(new PropertyValueFactory<Sort, String>("quantity"));
    sort_total.setCellValueFactory(new PropertyValueFactory<Sort, String>("total"));

    sort_view.getItems().add(data);


}

}

and my fxml file is as follows.

enter image description here

My Table is no populating, what could be the reason. Table shows as follows..

enter image description here

please look into it whats the problem...


Solution

  • It was just caused not proper use of naming convention,because there were multiple item's i had to use data.addAll(sort) instead of data.add(sort).