JavaFx Tableview复选框需要关注 [英] JavaFx Tableview checkbox requires focus

查看:79
本文介绍了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天全站免登陆