代码之家  ›  专栏  ›  技术社区  ›  Ranjit Vamadevan

我无法使用fxml填充javafx tableview

  •  1
  • Ranjit Vamadevan  · 技术社区  · 7 年前

    代码似乎没什么问题,但表没有填充。同一代码在其他地方工作得很好。

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

    }

    我的fxml文件如下。

    enter image description here

    我的桌子不填充,原因可能是什么。 下表如下所示。

    enter image description here

    请查查有什么问题…

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ranjit Vamadevan    7 年前

    这是由于命名约定的使用不当,因为存在多个项 我必须使用data.addall(排序)而不是data.add(排序)。

    推荐文章