Mateusz Gaweł
Mateusz Gaweł

Reputation: 683

JavaFx Tableview checkbox requires focus

I implemented boolean representation in my tableView as checkbox. It works fine in general but very irritating fact is that it requires row to be focused (editing) to apply change of checkbox value. It means I first have to double click on the field and then click checkbox.

How to make checkbox change perform onEditCommit right away?

public class BooleanCell<T> extends TableCell<T, Boolean> {
private CheckBox checkBox;

public BooleanCell() {
    checkBox = new CheckBox();
    checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if (isEditing())
                commitEdit(newValue == null ? false : newValue);
        }
    });
    setAlignment(Pos.CENTER);
    this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    this.setEditable(true);
}

@Override
public void updateItem(Boolean item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setGraphic(null);
    } else {
        checkBox.setSelected(item);
        setGraphic(checkBox);
    }
}

}

Upvotes: 0

Views: 544

Answers (1)

Zephyr
Zephyr

Reputation: 10253

I'm not sure about the rest of your implementation, but I assume you do not have your TableView set to editable:

tableView.setEditable(true);

On a side note, you could easily use a CheckBoxTableCell instead of implementing your own (BooleanCell).

Here is a very simple application you can run to see how this works. Each CheckBox may be clicked without first focusing the row and its value updates your underlying model as well (which you can see by clicking the "Print List" button).

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxTableViewSample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // List of sample items
        ObservableList<MyItem> myItems = FXCollections.observableArrayList();
        myItems.addAll(
                new MyItem(false, "Item 1"),
                new MyItem(false, "Item 2"),
                new MyItem(true, "Item 3"),
                new MyItem(false, "Item 4"),
                new MyItem(false, "Item 5")
        );

        // Create TableView
        TableView<MyItem> tableView = new TableView<MyItem>();

        // We need the TableView to be editable in order to allow each CheckBox to be selectable
        tableView.setEditable(true);

        // Create our table Columns
        TableColumn<MyItem, Boolean> colSelected = new TableColumn<>("Selected");
        TableColumn<MyItem, String> colName = new TableColumn<>("Name");

        // Bind the columns with our model's properties
        colSelected.setCellValueFactory(f -> f.getValue().selectedProperty());
        colName.setCellValueFactory(f -> f.getValue().nameProperty());

        // Set the CellFactory to use a CheckBoxTableCell
        colSelected.setCellFactory(param -> {
            return new CheckBoxTableCell<MyItem, Boolean>();
        });

        // Add our columns to the TableView
        tableView.getColumns().addAll(colSelected, colName);

        // Set our items to the TableView
        tableView.setItems(myItems);

        // Create a button to print out our list of items
        Button btnPrint = new Button("Print List");
        btnPrint.setOnAction(event -> {
            System.out.println("-------------");
            for (MyItem item : myItems) {
                System.out.println(item.getName() + " = " + item.isSelected());
            }
        });

        root.getChildren().addAll(tableView, btnPrint);
        // Show the Stage
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

/**
 * Just a simple sample class to display in our TableView
 */
final class MyItem {

    // This property will be bound to the CheckBoxTableCell
    private final BooleanProperty selected = new SimpleBooleanProperty();

    // The name of our Item
    private final StringProperty name = new SimpleStringProperty();

    public MyItem(boolean selected, String name) {
        this.selected.setValue(selected);
        this.name.set(name);
    }

    public boolean isSelected() {
        return selected.get();
    }

    public BooleanProperty selectedProperty() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected.set(selected);
    }

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

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }
}

Upvotes: 5

Related Questions