Event on ComBox which inside TableView cell JavaFx?

夙愿已清 提交于 2020-04-06 18:41:26

问题


I have TableView and when my program start TableView has only one row, also I have ArrayList of ComboBoxes and create one ComboBox for each row in the TableView, when user edit (Product Name) cell (which should contain the ComboBox on it) program create ComboBox for this cell and set the ComboBox as cell graphic (when ComboBox created not removed from cell graphic), and when the user select item from the last row ComboBox the program must create one row, the program behave correctly for the first row ComboBox action but the second row it failed, note the last ComboBox only its action create one additional row not every ComboBox, this photo can help to illustrate my problem

this my code:

import javafx.application.Application;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.TableCell;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class BillDesign extends Application
    {
        private final TableView <Products> tableProducts = new TableView();
        private final TableColumn serial = new TableColumn("Serial");
        private final TableColumn productName = new TableColumn("ProductName");
        private final ObservableList <Products> data = FXCollections.observableArrayList(new Products("1", ""),
                                                                                         new Products("2", ""),
                                                                                         new Products("3", ""),
                                                                                         new Products("4", ""));
        private final Button button = new Button("Click");

        @Override
        public void start(Stage stage)
        {
            serial.setEditable(true);
            serial.setCellValueFactory(new PropertyValueFactory("Serial"));

            productName.setEditable(true);
            productName.setCellValueFactory(new PropertyValueFactory("ProductName"));
            productName.setCellFactory(column -> new TableCell ()
            {
                private ComboBox comboBox;

                @Override
                public void startEdit()
                {
                    if(!isEmpty())
                    {
                        super.startEdit();

                        comboBox = new ComboBox();

                        comboBox.setPrefWidth(getWidth());
                        comboBox.setEditable(true);
                        comboBox.getItems().addAll("A", "B", "C");

                        setGraphic(comboBox);

                        comboBox.setOnAction(e ->
                        {
                            data.get(getIndex()).setProductName(comboBox.getSelectionModel().getSelectedItem().
                                                                toString());

                            if(getIndex() == data.size() - 1)
                            {
                                data.add(new Products(String.valueOf(data.size() + 1), ""));
                            }
                        });
                    }
                }
            });

            button.setOnMouseClicked(e ->
            {
                for(int i = 0; i < data.size(); i++)
                {
                    System.out.println("Name " + data.get(i).getProductName() + "\tSerial " + data.get(i).getSerial());
                }
            });

            tableProducts.setEditable(true);
            tableProducts.getColumns().addAll(serial, productName);
            tableProducts.setItems(data);

            VBox root = new VBox(tableProducts, button);
            Scene scene = new Scene(root);

            stage.setScene(scene);
            stage.show();
        }

        public class Products
        {
            private final SimpleStringProperty Serial;
            private final SimpleStringProperty ProductName;

            public Products(String serial, String productName)
            {
                this.Serial = new SimpleStringProperty(serial);
                this.ProductName = new SimpleStringProperty(productName);
            }

            public void setSerial(String serial)
            {
                Serial.set(serial);
            }

            public String getSerial()
            {
                return Serial.get();
            }

            public void setProductName(String productName)
            {
                ProductName.set(productName);
            }

            public String getProductName()
            {
                return ProductName.get();
            }
        }
    }

来源:https://stackoverflow.com/questions/60598826/event-on-combox-which-inside-tableview-cell-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!