JavaFx Tableview 复选框需要焦点 [英] JavaFx Tableview checkbox requires focus

查看:66
本文介绍了JavaFx Tableview 复选框需要焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 tableView 中实现了布尔表示作为复选框.它在一般情况下工作正常,但非常令人恼火的事实是它需要关注行(编辑)以应用复选框值的更改.这意味着我首先必须双击该字段,然后单击复选框.

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.

如何使复选框更改立即执行 onEditCommit?

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

}

推荐答案

我不确定您的其余实现,但我假设您没有将 TableView 设置为可

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

<小时>

顺便说一句,您可以轻松地使用 CheckBoxTableCell 而不是实现自己的 (BooleanCell).


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

这是一个非常简单的应用程序,您可以运行它来查看其工作原理.每个 CheckBox 都可以在不首先聚焦行的情况下点击,它的值也会更新您的基础模型(您可以通过点击打印列表"按钮看到).

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

这篇关于JavaFx Tableview 复选框需要焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆