0

The end goal that I am trying to accomplish is to extend the CheckBoxTableCell class so that I can bake in a toggle event listener with the space bar so that when I traverse the table with the arrow keys and focus a column with checkboxes, I can hit the space bar and it toggles the checkmark and of course sets the object's boolean property behind it. I am fairly new to JavaFX, but not Java and this has been a terribly hard problem to solve for me.

Here is the model

    public static class Person {
        private final SimpleStringProperty name;
        private final SimpleStringProperty email;
        private final BooleanProperty active;

        public Person(String name, String email, boolean active) {
            this.name = new SimpleStringProperty(name);
            this.email = new SimpleStringProperty(email);
            this.active = new SimpleBooleanProperty(active);
        }

        public String getName() {
            return name.get();
        }

        public SimpleStringProperty nameProperty() {
            return name;
        }

        public String getEmail() {
            return email.get();
        }

        public SimpleStringProperty emailProperty() {
            return email;
        }

        public BooleanProperty activeProperty() {
            return active;
        }
    }

Here is the implementation I am working with.

Here I am trying just to try and get it work using the TableView API but ultimately I want to encapsulate the functionality in the extended CheckBoxTableCell class so when I build the cell factory of those types, it has that built in.

 @Override
    public void start(Stage primaryStage) throws Exception {

            TableView<Person> tableView = new TableView<>();
            TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
            TableColumn<Person, String> emailColumn = new TableColumn<>("Email");
            TableColumn<Person, Boolean> activeColumn = new TableColumn<>("Active");

            nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
            emailColumn.setCellValueFactory(new PropertyValueFactory<>("email"));
            nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
            emailColumn.setCellFactory(TextFieldTableCell.forTableColumn());
            
            activeColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty());
            activeColumn.setCellFactory(CheckBoxTableCell.forTableColumn(activeColumn));

            tableView.setEditable(true);
            tableView.getColumns().addAll(nameColumn, emailColumn, activeColumn);
            

            // Sample data
            tableView.getItems().addAll(
                    new Person("John", "john@example.com", true),
                    new Person("Jane", "jane@example.com", false),
                    new Person("Michael", "michael@example.com", true)
            );


//  I need the checkbox column to be toggleable with the spacebar.
            tableView.getSelectionModel().setCellSelectionEnabled(true);
            tableView.setOnKeyPressed(new EventHandler<KeyEvent>() {
                
                @Override
                public void handle(KeyEvent event) {
                    TableColumn col = tableView.getColumns().get(tableView.getFocusModel().getFocusedCell().getColumn());
                    if (event.getCode() == KeyCode.SPACE) {
                          //HOW IN THE WORLD CAN I TARGET THE CHECKBOX TO SWAP THE FLAG
                    }
                }
             });
            StackPane root = new StackPane(tableView);
            Scene scene = new Scene(root, 400, 300);

            primaryStage.setScene(scene);
            primaryStage.setTitle("Editable TableView Example");
            primaryStage.show();
        
        
    }
Anden
  • 43
  • 6
  • 2
    In this complete [example](https://stackoverflow.com/a/68969223/230513), the tab key moves among the check boxes and the space key toggles the state. See also [_Why should I avoid using PropertyValueFactory in JavaFX?_](https://stackoverflow.com/q/72437983/230513) – trashgod Aug 20 '23 at 12:42

0 Answers0