请解释如何使用CheckBoxTableCell

2024-03-15

我想了解更多有关如何实际使用或子类化(如果需要)CheckBoxTableCell 的信息。在一种特定情况下,我想使用此类,其中复选框不绑定到基础数据模型属性。

假设我有一个名为“选择”的列,其中包含复选框。该列或多或少充当该行的视觉标记。用户可以勾选这些框,然后使用按钮对勾选的行执行某些操作(例如删除所有勾选的行)。

我已经搜索过有关此主题的教程。虽然有一些教程或解释,但它们涉及一些与复选框相关的支持数据模型。

因此,我正在寻找对此的更详细解释,其中复选框是动态生成的,并充当用户界面的辅助辅助,例如上面解释的示例。此外,我想知道编辑如何发挥作用并根据标准和约定正确编码,特别是当 Java 8 引入属性更新、新的 javafx 类等时。

如果有帮助的话,一个常见的参考示例可以是“Person”的数据模型,它只有一个属性“Name”。 ObservableList 可以绑定到显示名称的 TableView。设置在(表格视图)最左侧的另一列是针对每个名称的复选框。最后,至少有一个按钮允许对人员列表进行某种形式的操作。为了简单起见,该按钮只是根据按钮被操作时是否在人员姓名上标记了勾号来删除列表中的人员。此外,可以制作一个按钮来添加新人,但它可以是可选的。

我希望我已经以简洁详细的方式写了主题,并且有一个明确的解决方案。预先感谢任何可以提供相关信息的人。


使用数据模型Person如上所述的示例,布尔属性,例如添加了已注册状态,因此名为“已注册”的第三个表列被添加到 TableView。

考虑代码示例:

//The Data Model
public class Person
{
    /*
     * Fields
     */
    private StringProperty firstName;

    private StringProperty lastName;

    private BooleanProperty registered;


    /* 
     * Constructors
     */
    public Person(String firstName, String lastName, boolean registered)
    {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
        this.registered = new SimpleBooleanProperty(registered);
    }

    public Person()
    {
        this(null, null, false);
    }

    /*
     * Properties
     */

    public StringProperty firstNameProperty() { return firstName; }

    public String getFirstName() { return this.firstName.get(); }

    public void setFirstName(String value) { this.firstName.set(value); }


    public StringProperty lastNameProperty() { return lastName; }

    public String getLastName() { return this.lastName.get(); }

    public void setLastName(String value) { this.lastName.set(value); }


    public BooleanProperty registeredProperty() { return registered; }

    public boolean isRegistered() { return this.registered.get(); }

    public void setRegistered(boolean value) { this.registered.set(value); }
}
//Dummy values for the data model
List<Person> personList = new ArrayList<Person>();
personList.add( new Person("John", "Smith", true) ;
personList.add( new Person("Jack", "Smith", false) );
TableView<Person> tblView = new TableView<Person>();

tblView.setItems( FXCollections.observableList(personList) );

TableColumn firstName_col = new TableColumn("First Name");
TableColumn lastName_col = new TableColumn("Last Name");
TableColumn registered_col = new TableColumn("Registered");

firstName.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
lastName.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));

registered_col.setCellValueFactory(
new Callback<CellDataFeatures<Person,Boolean>,ObservableValue<Boolean>>()
{
    //This callback tell the cell how to bind the data model 'Registered' property to
    //the cell, itself.
    @Override
    public ObservableValue<Boolean> call(CellDataFeatures<Person, Boolean> param)
    {   
        return param.getValue().registeredProperty();
    }   
});

//This tell how to insert and render a checkbox in the cell.
//
//The CheckBoxTableCell has the updateItem() method which by default links up the
//cell value (i.e. the 'Registered' property to the checkbox.  And this method is
//automatically call at the appropriate time, such as when creating and rendering
//the cell (I believe).
//
//In this case, as the registed_col.setCellValueFactory() method has specified
//'Registered' in the actual data model (i.e. personList), therefore the checkbox will
//be bound to this property.
registered_col.setCellFactory( CheckBoxTableCell.forTableColumn(registered_col) );

tblView.getColumns.addAll(firstName_col, lastName_col, registered_col);

//table display preference - should not affect this exercise/problem
tblView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

这段代码工作正常。当迭代数据模型或tblView用于访问的 UI 组件Registered属性,它将显示正确的值,即使它发生变化(即取消/勾选复选框)。

尝试添加不绑定到数据模型的复选框的原始问题尚未得到解答。

假设添加了另一列调用“选择”,并且它仅包含一个复选框以直观地指示可以(或已)选择一行或多行。重申一下,此列复选框与数据模型没有任何相关含义Person。从而在内部创建一个属性Person保存该值的类在语义上是不必要的,并且可能被认为是糟糕的编码实践。那么这个问题是如何解决的呢?

如何将任意 BooleanProperty(或 personList 中每个人的列表)链接或绑定到相应的复选框?

TableColumn select_col = new TableColumn("Select");

//Set a boolean property to represent cell value.
select_col.setCellValueFactory(
new Callback<CellDataFeatures<Person,Boolean>,ObservableValue<Boolean>>()
{
    @Override
    public ObservableValue<Boolean> call(CellDataFeatures<Person,Boolean> param)
    {
        //PROBLEM -- What Code goes here?
    }
};

//This call should be okay - it would display the checkbox according to the provided
//boolean (property).  This was proven with 
//registered_col.setCellFactory(CheckBoxTableCell.forTableColumn(registered_col)
select_col.setCellFactory(CheckBoxTableCell.forTableColumn(select_col);

一种解决方案是创建一个(匿名)内部类,该内部类可以子类化Person并添加“选择”属性。使用与“注册”属性及其表列类似的代码来链接“选择”属性,它应该可以工作。如上所述,仅仅为了解决视觉问题而进行子类化就破坏了数据模型语义。

更好的解决方案可能是 - 为每个人创建一个布尔属性的内部列表personList并将它们连接在一起。那么如何检索与每个人相对应的适当布尔属性personList in the setCellValueFactory()方法?一种可能的解决方案是使用索引位置personList、选择列的布尔属性列表以及行索引。所以归结为获取行索引setCellValueFactory(CellDataFeatures),以及如何正确完成此操作?

考虑代码:

TableColumn<Person,Boolean> select_col = new TableColumn<Person,Boolea>("Select");

List<BooleanProperty> selectedRowList = new ArrayList<BooleanProperty>();

//This callback allows the checkbox in the column to access selectedRowList (or more
//exactly, the boolean property it contains
Callback<Integer,ObservableValue<Boolean>> selectedStateSelectColumn =
    new Callback<Integer,ObservableValue<Boolean>>()
{

    //index in this context reference the table cell index (I believe)
    @Override
    public ObservableValue<Boolean> call(Integer index)
    {
        return selectedRowList.get(index);
    }
}

//Initialise the selectedRowList
for(Person p : personList)
{
    //initially, it starts off as false, i.e. unticked state
    selectedRowList.add( new SimpleBooleanProperty() ); 
}

select_col.setCellValueFactory(
    new Callback<CellDataFeatures<Person,Boolean>,ObservableValue<Boolean>>
{
    //retrieve the cell index and use it get boolean property in the selectedRowList
    @Override
    public ObservableValue<Boolean> call(CellDataFeatures<Person,Boolean> cdf)
    {
        TableView<Person> tblView = cdf.getTableView();

        Person rowData = cdf.getValue();

        int rowIndex = tblView.getItems().index( rowData );

        return selectedRowList.get( rowIndex );
    }
}

select_col.setCellFactory(
    CheckBoxTableCell.forTableColumn(selectedStateSelectColumn));

这些片段对我有用。只需要重新组织即可编译和运行。不过,要点部分是正确的。

这个问题或情况很常见,但我花了几天时间来实施和解决。我希望这对其他人有好处。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

请解释如何使用CheckBoxTableCell 的相关文章

  • 如何为我的代码启动一个线程并为 JavaFX 应用程序启动一个线程?

    我正在尝试使用 JavaFX 运行程序 如果我使用 Swing 我将有一个由 main 方法启动的类 并让它构建 GUI 类 这将为我提供 2 个线程 一个是应用程序的普通线程 另一个是 EventQueue 这将防止阻塞 UI 工作 因此
  • 窗格形状修改

    好吧 长话短说 我正在尝试创建一种聊天 消息系统 并且需要一点帮助 我正在尝试在容器上创建一个箭头 如下图所示 该图像是从 ControlsFX 及其 PopOver 窗口中取出的 我不能使用他们的弹出窗口小部件 因为它的行为与我使用它的目
  • 从 Javafx2.2 迁移到 Javafx8

    我正在尝试将 Javafx 2 2 应用程序迁移到 Javafx 8 我在使用嵌套时遇到以下问题FXML javafx fxml LoadException Root hasn t been set Use method setRoot b
  • 禁用 JavaFX 图表背景图像的缓存

    我有一个简单的折线图 按下按钮即可在新窗口中打开 该折线图使用存储在硬盘上的图像作为背景 如果我关闭计算折线图的窗口 更改图像文件 或删除它 并重新打开窗口 则会再次加载旧图像 我在场景生成器和代码中禁用了折线图的缓存 但这没有帮助 有人能
  • 如何将 JavaFX TableView 与 java 记录一起使用?

    Records是一个新功能Java 16 https en wikipedia org wiki Java version history Java 16 定义于JEP 395 记录 https openjdk org jeps 395 假
  • 在 JavaFX 中使用 MouseEvent 和 MouseClicked 选择并移动 Canvas 图像

    我有一个应用程序的示例 用于绘制图片GraphicsContext并如下图所示工作 问题是select and move只有blue circle水平地与Canvas MouseEvent and MouseClicked public c
  • Titan 用新数据刷新 TableView

    这就是我正在尝试做的 tableView data 0 rows selectedPosY children selectedPosX imageId tempImageId tableView data 0 rows selectedPo
  • javafx大图像崩溃

    JavaFX 新手 此示例适用于小图像 但是大图像会使 ImageView 崩溃 我的示例代码有缺陷吗 JavaFX 中的大图像有问题吗 还有别的事吗 我从网上抓了一个例子 http www java2s com Code Java Jav
  • JavaFX 8 - 如何将 TextField 文本属性绑定到 TableView 整数属性

    假设我有这样的情况 我有一个TableView 表作者 有两个TableColumns 身份证号和姓名 这是 AuthorProps POJO 由TableView import javafx beans property SimpleIn
  • 在胶子mapLayer中创建折线

    Google 地图 API 可以在地图上创建包含连接点的折线的图层 我搜索了在哪里可以找到 gluon 的 mapLayer 的示例或实现 请指教 虽然没有明确的 API 用于在对象之上绘制直线 折线或多边形MapView the MapL
  • 条件绑定

    我是 JavaFx 新手 我正在创建一个应用程序 用户必须填写一些表单 并且我想使用绑定 预先验证 它们 简单的事情 比如所有元素都不能为空 或者其中一些元素只能包含数字 这是我到目前为止所拥有的 saveBtn disableProper
  • JavaFX 虚拟键盘不显示

    我是javafx新手 我制作了一个简单的应用程序表单 它有 锚定窗格 Pane 文本域 我在触摸屏设备上运行该应用程序 但虚拟键盘不显示 文本字段已经聚焦 我使用的是 JDK 8u25 场景生成器 2 0 根据我读到的 http docs
  • 如何使用 JavaFX 中的 JCSG 库将 MeshView 转换为 CSG 对象

    我正在使用 JavaFX 的 JCSG 库 我有一些MeshView我想将它们转换成的对象CSG对象 有办法实现吗 最简单的方法是组合javafx scene shape Mesh对象与 CSG 对象 前提是您有TriangleMesh正在
  • 使用多个值过滤 JFX TableView

    我目前正在尝试过滤我的数据TableView using FilteredList with predicate 我有2个ComboBoxes来过滤值 我的表包含Result Each Result has a Student that S
  • 根据 Swift 中的列表选择在 ViewController 之间传递值

    我试图将 listView 选择的选定索引号从一个 ViewController 传递到另一个 ViewController 但遇到了 tableView didSelectRowAtIndexPath 委托运行时间稍晚于prepareFo
  • 如何在 TableRow 的一个单元格中添加超过 1 个视图?

    如上所述 如何将 2 个视图放入一个单元格中tablerow 我创建了一个表格布局 并通过代码添加行 下面是我的代码 TableLayout v TableLayout inflater inflate R layout featureit
  • 带有地图的 JavaFX TableView 对象

    因此 我对 JavaFx TableView 进行了一些挖掘 并找到了一些针对简单情况的不错的解决方案 This article http docs oracle com javafx 2 ui controls table view ht
  • javafx tableview 中的快速过滤

    我需要在 javafx tableview 中实现一个具有大量数据 大约 100 000 的过滤器 我努力了本教程 http code makery ch blog javafx 2 tableview filter 它可以工作 但是与 s
  • JavaFX 中具有自定义内容的 ListView

    How i can make custom ListView with JavaFx for my app I need HBox with image and 2 Labels for each line listView 您可以通过查看
  • Javafx过滤表视图

    我正在尝试使用文本字段来过滤表视图 我想要一个文本字段 txtSearch 来搜索 nhs 号码 名字 姓氏 和 分类类别 我尝试过在线实施各种解决方案 但没有运气 我对这一切仍然很陌生 所以如果问得不好 我深表歉意 任何帮助将不胜感激 我

随机推荐