首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在JavaFX中使用CheckBox更新TableRow

,可以通过以下步骤实现:

  1. 首先,创建一个JavaFX应用程序,并导入所需的包。
代码语言:txt
复制
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
  1. 创建一个数据模型类,用于存储表格数据。
代码语言:txt
复制
public class Person {
    private String name;
    private boolean selected;

    public Person(String name) {
        this.name = name;
        this.selected = false;
    }

    public String getName() {
        return name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}
  1. 在主应用程序类中,创建一个TableView并设置列和数据。
代码语言:txt
复制
public class MainApp extends Application {
    private ObservableList<Person> data = FXCollections.observableArrayList();

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> table = new TableView<>();
        table.setEditable(true);

        TableColumn<Person, String> nameCol = new TableColumn<>("Name");
        nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());

        TableColumn<Person, Boolean> selectCol = new TableColumn<>("Select");
        selectCol.setCellValueFactory(cellData -> {
            Person person = cellData.getValue();
            SimpleBooleanProperty property = new SimpleBooleanProperty(person.isSelected());
            property.addListener((observable, oldValue, newValue) -> person.setSelected(newValue));
            return property;
        });
        selectCol.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
            @Override
            public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> param) {
                return new CheckBoxTableCell<>();
            }
        });

        table.getColumns().addAll(nameCol, selectCol);
        table.setItems(data);

        VBox vbox = new VBox(table);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  1. 创建一个自定义的TableCell,用于显示CheckBox。
代码语言:txt
复制
public class CheckBoxTableCell<S, T> extends TableCell<S, T> {
    private final CheckBox checkBox;

    public CheckBoxTableCell() {
        this.checkBox = new CheckBox();
        this.checkBox.setOnAction(event -> {
            S rowData = getTableView().getItems().get(getIndex());
            TableColumn<S, T> column = getTableColumn();
            ObservableValue<T> observableValue = column.getCellObservableValue(rowData);
            T value = observableValue.getValue();
            if (value instanceof Boolean) {
                column.getCellObservableValue(rowData).setValue((T) checkBox.isSelected());
            }
        });
        setGraphic(checkBox);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setEditable(true);
    }

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        } else {
            checkBox.setSelected((Boolean) item);
            setGraphic(checkBox);
        }
    }
}
  1. 运行应用程序,将会显示一个带有CheckBox的表格。当用户选择或取消选择CheckBox时,对应行的数据模型中的selected属性将会更新。

这是一个使用JavaFX中的CheckBox更新TableRow的示例。在这个示例中,我们创建了一个TableView,并在其中的一列中使用了CheckBox。当用户选择或取消选择CheckBox时,对应行的数据模型中的selected属性将会更新。这个示例可以用于实现一些需要用户选择多个行的功能,例如批量操作或筛选数据。

腾讯云相关产品和产品介绍链接地址:

请注意,以上链接仅供参考,具体产品选择应根据实际需求进行评估和决策。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

10分25秒

19-尚硅谷-在Eclipse中使用Git-更新本地库

6分26秒

30-尚硅谷-在Idea中使用Git-更新本地库

31分16秒

10.使用 Utils 在列表中请求图片.avi

23分54秒

JavaScript教程-48-JSON在开发中的使用【动力节点】

11分37秒

107.使用Image-Loader在ListView中请求图片.avi

22分4秒

87.使用Volley在ListView或者GridView中请求图片.avi

11分50秒

JavaScript教程-49-JSON在开发中的使用2【动力节点】

8分26秒

JavaScript教程-50-JSON在开发中的使用3【动力节点】

4分21秒

JavaScript教程-51-JSON在开发中的使用4【动力节点】

19分33秒

JavaScript教程-52-JSON在开发中的使用5【动力节点】

7分58秒

21-基本使用-Nginx反向代理在企业中的应用场景

1分53秒

在Python 3.2中使用OAuth导入失败的问题与解决方案

领券