Флажок JavaFx Tableview требует фокуса - PullRequest
0 голосов
/ 18 ноября 2018

Я реализовал логическое представление в моем tableView как флажок.В целом работает нормально, но очень раздражает тот факт, что для изменения значения флажка требуется фокусировка строки (редактирование).Это означает, что мне сначала нужно дважды щелкнуть по полю, а затем щелкнуть флажок.

Как сделать так, чтобы изменение флажка сразу же выполнялось на EditCommit?

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);
    }
}

}

1 Ответ

0 голосов
/ 18 ноября 2018

Я не уверен насчет остальной части вашей реализации, но я предполагаю, что у вас TableView не установлен для редактирования:

tableView.setEditable(true);

В дополнение к этому, вы можете легко использовать CheckBoxTableCell вместо реализации своего собственного (BooleanCell).

Вот очень простое приложение, которое вы можете запустить, чтобы увидеть, как это работает. Каждый CheckBox может быть нажат без предварительной фокусировки строки, и его значение также обновляет вашу базовую модель (которую вы можете увидеть, нажав кнопку «Печать списка»).

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);
    }
}
...