How to add dynamic row value from button to tableview in javafx

43 Views Asked by At

I am working on a POS system using Javafx , and I'm trying to set value to a tableview after clicking a button which was generated by MySQL data to. But any time I click the button let say coffee it's fetching all data from instead.

Here is the code

public void allItems(String EnterItemName) throws SQLException {
    String query ="SELECT `id`,`name`,`rate`FROM `products` WHERE `category`= '"+EnterItemName+"' ORDER BY `rate` ";
    buttonList = new ArrayList<>(); //our Collection to hold newly created Buttons
    pst = con.prepareStatement(query);
    rs = pst.executeQuery();
    while (rs.next()){
        String id = rs.getString(1);
        String name = rs.getString(2);
        String rate = rs.getString(3);
        ArrayList<String> content= new ArrayList<>();
        content.add(id);
        content.add(name);
        content.add(rate);
        JFXButton buttonFist = new JFXButton( content.get(1)+"\n "+content.get(2) +" $");
        buttonFist.setStyle(" -fx-background-color:  #005d9a;" +
                " -fx-pref-height: 50px;\n" +
                "-fx-pref-width: 150px;" +
                "-fx-text-fill:#ffffff;"+
                "-fx-alignment:CENTER;");
        buttonFist.setOnAction(event1 ->  {
            btnClicked ++;
           try {
               double priceProd = Double.parseDouble(content.get(2)) ;
                      lblProdPrice.setText(String.valueOf(priceProd));
               int qte = btnClicked;
               lblProdQte.setText(String.valueOf(qte));
               double total = qte * priceProd;
               lblTotalPrice.setText(String.valueOf(total));
                dataCollection = getObsArticles();
                colIDArticle.setCellValueFactory(new PropertyValueFactory<>("item_Code"));
                colArticleName.setCellValueFactory(new PropertyValueFactory<>("item_Description"));
                colArticlePrice.setCellValueFactory(new PropertyValueFactory<>("item_Price"));
                colArticleQt .setCellValueFactory(new PropertyValueFactory<>("item_Qty"));
                colArticleTotal.setCellValueFactory(new PropertyValueFactory<>("amount"));
                tableArticle.setItems(dataCollection);
                String order_Id=content.get(0);
                String itemCode=content.get(0);
                int qty= btnClicked;
                double unitPrice= Double.parseDouble(content.get(2));
                OrderDetail od=new OrderDetail(order_Id,itemCode,qty,unitPrice);
                orderDetailList.add(od);
                            }catch (Exception e){
                e.printStackTrace();
            }
            currentTable =tableArticle.getItems();
            String currentProdId = String.valueOf(content.get(0));
            if (event1.getSource()!=content.get(0)){
                               double value =Double.parseDouble( content.get(2));
                if (event1.getSource().equals(content.get(0))){
                }else if (event1.getSource()!=content.get(0)){
                    int frstindex  = event1.getSource().toString().indexOf("<b>");
                    double Gtotal = Double.parseDouble(txtTotalPOS.getText())  + value;
                    txtTotalPOS.setText(Double.toString(Gtotal));
                    txtSubTotalPOS.setText(Double.toString(Gtotal));
                }
            }
            for (OrderTableModel product: currentTable){
                if (product.getItem_Code().equals(currentProdId)){
                    product.setItem_Code(content.get(0));
                    product.setItem_Description(content.get(1));
                    product.setItem_Price(Double.parseDouble(content.get(2)));
                    product.setItem_Qty(btnClicked);
                    tableArticle.setItems(currentTable);
                    break;
                }
            }
        });
        buttonList.add(buttonFist);
    }
    hboxx.setAlignment(Pos.CENTER);
    hboxx.getChildren().clear();
    hboxx.getChildren().addAll(buttonList);
 }

Point of sale:

0

There are 0 best solutions below