我有两个带有两个不同FXML视图的不同控制器. 第一个控制器是CashierController,内部有一个用于打开新场景的按钮,并带有新的控制器AddProductController.在此收银员控制器中,was购物车会初始化,以根据购物车项目填充表格视图.
I have two different controller with two different FXML view. First controller is CashierController,there is button inside to open new scene and with new controller AddProductController. In this cashiercontroller the was cart to initialize to populate the tableview based on cart item.
第二,在上一个收银台控制器仍处于打开状态时,单击带有新场景的按钮打开Addproductcontroller.在AddProductController内有网格窗格,选择网格窗格内的任何单元格会将产品添加到购物车中.
Second, open Addproductcontroller when click that button with new scene while prev cashiercontroller still open.inside AddProductController have the gridpane,when choose any cell inside the gridpane will add the product to cart.
问题是购物车未更新,由于CashierController中的方法,我无法使用它来刷新列表和重建表.我想如何将数据传递回仍处于打开状态的前一个控制器,而我仍在当前状态(addproductController).如何在2个控制器之间进行通信以接收和/或侦听从新场景到上一个场景的事件.
The problem was the the cart not update and i cant use to refresh the list and rebuild the table because method in CashierController. How i want to pass back the data to previous controller that still open, while i still at current(addproductController).How to communicate the between 2 controller to receive back and or listen event from new to prev scene.
public class CashierController implements Initializable { public Button btnAdd; public JFXButton btnExit; public TableView<Item> tableView; public TableColumn<Item, String> colQty; public TableColumn<Item, String> colName; public TableColumn<Item, String> colPrice; public JFXTextField txtBarcode; @FXML private Label lblAmount; @FXML private Label lblInfo; Cart cart; ProductModel productModel; // private CashierController (){} // public static CashierController getInstance(){ return CashierControllerHolder.INSTANCE; } private static class CashierControllerHolder { private static final CashierController INSTANCE = new CashierController(); } @Override public void initialize(URL location, ResourceBundle resources) { productModel = new ProductModel(); cart = new Cart(); } public void calculateView() { lblSubtotal.setText(cart.getSubTotal().toString()); lblTax.setText(cart.getChargesTax().toString()); lblTotal.setText(cart.getTotal().toString()); } public void refreshList() { ObservableList<Item> items = FXCollections.observableArrayList(cart.getItemList()); setCellTable(); tableView.setItems(items); } public void addToCart(Product product) { cart.add(product.getId(), product.getBarcode(),product.getName(), 1, product.getPrice_sell()); } public void addProductAction(ActionEvent actionEvent) { try { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/subview/addProduct.fxml")); Parent root1 = (Parent) fxmlLoader.load(); Stage stage = new Stage(); stage.setScene(new Scene(root1)); AddProductController addProductControllerController = fxmlLoader.getController(); fxmlLoader.setController(addProductControllerController); stage.show(); } catch (IOException e) { e.printStackTrace(); } } public void setCellTable(){ colQty.setCellValueFactory(new PropertyValueFactory<>("quantity")); colName.setCellValueFactory(new PropertyValueFactory<>("name")); colPrice.setCellValueFactory( cellData ->{ SimpleStringProperty property = new SimpleStringProperty(); property.set(df1.format(cellData.getValue().getTotal())); return property; }); } }这是AddProducController
this is AddProducController
public class AddProductController implements Initializable , ProductInterface { @FXML public Tab tabAll; @FXML public BorderPane borderPaneAll; public JFXButton btnExit; private GridPane gridPane = new GridPane(); private ProductModel productModel; @Override public void initialize(URL location, ResourceBundle resources) { productModel = new ProductModel(); loadData(); Label[] label = new Label[PRODUCTLIST.size()]; Label[] labelBarcode = new Label[PRODUCTLIST.size()]; Label[] labelPrice = new Label[PRODUCTLIST.size()]; VBox vBoxes[] = new VBox[4]; int cols=2, colCnt = 0, rowCnt = 0; for (int i=0; i<PRODUCTLIST.size(); i++) { label[i] = new Label(); labelBarcode[i] = new Label(); labelPrice[i] = new Label(); label[i].setText(PRODUCTLIST.get(i).getName()); labelBarcode[i].setText(PRODUCTLIST.get(i).getBarcode()); labelPrice[i].setText(String.valueOf(PRODUCTLIST.get(i).getPrice_sell())); vBoxes[i] = new VBox(5); vBoxes[i].getChildren().addAll(label[i],labelBarcode[i], labelPrice[i]); gridPane.add(vBoxes[i], colCnt, rowCnt); gridPane.setHgap(20); gridPane.setVgap(20); colCnt++; if (colCnt>cols) { rowCnt++; colCnt=0; } } borderPaneAll.setCenter(gridPane); addGridEvent(); } private void addGridEvent() { gridPane.getChildren().forEach(item -> { item.setOnMouseClicked(new EventHandler<MouseEvent>() { String name = null; String barcode = null; BigDecimal price = new BigDecimal(0.00); List<String> t = new ArrayList<>(); @Override public void handle(MouseEvent event) { if (event.getClickCount() == 2) { if(item instanceof VBox){ for(Node nodeIn: ((VBox) item).getChildren()){ if(nodeIn instanceof Label){ t.add(((Label)nodeIn).getText()); } } } name = t.get(0); barcode = t.get(1); price = new BigDecimal(t.get(2)); Product product = productModel.getProductByBarcode(barcode); //add product to cart CashierController.getInstance().addToCart(product); //refreshthelist //calculatethevalue // CashierController.getInstance().refreshList(); // CashierController.getInstance().calculateView(); } if (event.isPrimaryButtonDown()) { System.out.println("PrimaryKey event"); } } }); }); } private void loadData(){ if (!PRODUCTLIST.isEmpty()) { PRODUCTLIST.clear(); } PRODUCTLIST.addAll(productModel.getProducts()); } }将产品添加到购物车是在网格窗格内单击的.我尝试使用静态实例,但在null异常中出错.预先感谢.
The add product to cart was in click inside gridpane. Im try use static instance but error in null exception. Thank in advance.
推荐答案更改
AddProductController addProductControllerController = fxmlLoader.getController(); fxmlLoader.setController(addProductControllerController); stage.show();到
AddProductController addProductControllerController = fxmlLoader.getController(); fxmlLoader.setController(addProductControllerController); stage.showAndWait();在"showAndWait"命令之后列出的所有代码将在运行之前等待对话框关闭.因此,从本质上讲,您可以为AddProductController返回的所有数据放置一个公共的getter方法,并在stage.showAndWait
All code listed after the "showAndWait" command will wait for the dialog to close before running. So essentially you can place a public getter method for whatever data your AddProductController is returning and call that in the main CashierController after the stage.showAndWait
类似这样的东西
AddProductController addProductControllerController = fxmlLoader.getController(); fxmlLoader.setController(addProductControllerController); stage.showAndWait(); Product product = addProductControllerController.getScannedProduct();更多推荐
javafx将数据发送回上一个控制器
发布评论