sshahine / JFoenix

JavaFX Material Design Library
MIT License
6.29k stars 1.06k forks source link

Toggle Button loses state in TableView #1209

Open ericwelshdev opened 3 years ago

ericwelshdev commented 3 years ago

In a nutshell , I have a JFXToggleButton in a TableView column - implemented via Class TableCell - of which works fine .. however after the table is loaded and you flip a toggle a few other random toggles also flip ( not intentional or by my code ) a second thing that was noticed was that as you scroll through the result the flipped toggles change state.. example - if I select row 1 and flip the toggle on and then scroll down within the list not only will I see other toggles flipped but then when I scroll back up row 1 is not longer flipped on position but row 6 is not flipped on when it wasn't before I scrolled down.


        class Person {

            private String firstName = null;
            private String lastName = null;

            public Person() {
            }

            public Person(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
            }

            public String getFirstName() {
                return firstName;
            }

            public void setFirstName(String firstName) {
                this.firstName = firstName;
            }

            public String getLastName() {
                return lastName;
            }

            public void setLastName(String lastName) {
                this.lastName = lastName;
            }
        }

        @Override
        public void start(Stage primaryStage) {

            TableView tableView = new TableView();

            TableColumn<Person, String> column1 = new TableColumn<>("First Name");
            column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

            TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
            column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

            tableView.getColumns().add(column1);
            tableView.getColumns().add(column2);
            addButtonToTableView(tableView);

            tableView.getItems().add(new Person("John", "Doe"));
            tableView.getItems().add(new Person("Jane", "Deer"));

            VBox vbox = new VBox(tableView);

            Scene scene = new Scene(vbox);

            primaryStage.setScene(scene);

            primaryStage.show();

        }

        public void addButtonToTableView(TableView tableView){
            //Adding the Button to the cell
            //Insert Button
            TableColumn col_action = new TableColumn<>("Action");
            tableView.getColumns().add(col_action);

            col_action.setCellValueFactory(
                    (Callback<TableColumn.CellDataFeatures<ObjectMapping, Boolean>, ObservableValue<Boolean>>) p -> new SimpleBooleanProperty(p.getValue() != null));

            //Adding the Button to the cell
            col_action.setCellFactory(
                    (Callback<TableColumn<ObjectMapping, Boolean>, TableCell<ObjectMapping, Boolean>>) p -> new ToggleButtonCell());
        }

        //Define the button cell
        private class ToggleButtonCell extends TableCell<ObjectMapping, Boolean> {
            final JFXToggleButton cellButton = new JFXToggleButton();

            ToggleButtonCell(){

                //Action when the button is pressed
                cellButton.setOnAction(t -> {
                    // get Selected Item
                    ObjectMapping currentRow = (ObjectMapping) ToggleButtonCell.this.getTableView().getItems().get(ToggleButtonCell.this.getIndex());
                    //remove selected item from the table list
                    if (currentRow != null) {
                        System.out.println("selectedData: " + currentRow.getSourceTableNm());
                    } else {
                        System.out.println("selected value was empty..");
                    }
                });
            }

            //Display button if the row is not empty
            @Override
            protected void updateItem(Boolean t, boolean empty) {
                super.updateItem(t, empty);
                if(!empty){
                    setGraphic(cellButton);
                }
            }
        }

Any thoughts as to how to get around this? Thanks!

ericwelshdev commented 3 years ago

Forgot to add I'm running JDK 13.0.6 jfoenix 9.0.10